Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Publish events via WebSocket - AWS AppSync Events

Publish events via WebSocket

AWS AppSync Events allows you to publish events via your API’s WebSocket endpoint after you connect to it.

Publish steps

  1. Connect to your WebSocket endpoint: wss://WS_DOMAIN/event/realtime.

  2. Send a publish message over the established WebSocket connection with the following information:

    • The authorization header(s) required to authorize your message.

    • The following message details:

      • A unique ID for your message.

      • The channel that you are publishing to.

      • The list of events that you are publishing. You can publish a maximum of five events in a batch.

Each specified event in your publish request must be a stringified valid JSON value.

Publish example

The following is an example of a request written in JavaScript that uses an API key for authorization.

const message = { id: 'uniqueID', // Your unique id for this message. type: 'publish', channel: '/default/channel', events: [ JSON.stringify({ message: "hello world" }), JSON.stringify({ number: 42 }) ], authorization: { 'x-api-key': 'your key' }, }

You can use your browser’s WebSocket API to connect and publish the events. The following simplified example uses an API key that sends the message. Notice that you don't need to subscribe to any channel before publishing.

const HTTP_DOMAIN = 'your api HTTP domain ' const REALTIME_DOMAIN = 'api real-time domain' const API_KEY = 'your api key' const authorization = { 'x-api-key': API_KEY, host: HTTP_DOMAIN } // construct the protocol header for the connection function getAuthProtocol() { const header = btoa(JSON.stringify(authorization)) .replace(/\+/g, '-') // Convert '+' to '-' .replace(/\//g, '_') // Convert '/' to '_' .replace(/=+$/, '') // Remove padding `=` return `header-${header}` } const socket = await new Promise((resolve, reject) => { const socket = new WebSocket(`wss://${REALTIME_DOMAIN}/event/realtime`, [ 'aws-appsync-event-ws', getAuthProtocol(), ]) socket.onopen = () => resolve(socket) socket.onclose = (event) => reject(new Error(event.reason)) socket.onmessage = (event) => console.log(event) }) // when the socket is connected, send the message socket.send(JSON.stringify(message))

You receive an acknowledgement message for every publish. For more information, see Understanding the Event API WebSocket protocol.

To learn more about the different authorization types that AWS AppSync Events supports, see Configuring authorization and authentication to secure Event APIs.

PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.