Originally published at https://ssojet.com/blog/news-2025-03-aws-appsync-events-websocket-pub
AWS has enhanced AppSync Events with direct message publishing over WebSocket connections, facilitating real-time communication. This fully-managed serverless WebSocket API service allows developers to broadcast event data to numerous subscribers seamlessly.
The initial launch of AWS AppSync Events enabled developers to easily broadcast real-time event data to millions of subscribers using secure and performant Serverless WebSocket APIs. With this new update, developers can utilize a single WebSocket connection for both publishing and receiving events, which significantly reduces implementation complexity.
Image courtesy of AWS Front-End Web & Mobile blog post
Darryl Ruggles, a cloud solutions architect and AWS Community Builder, noted on X that while other WebSocket approaches exist on AWS, AppSync Events works effectively for many scenarios.
Brice Pellé, a principal product manager at AWS, stated in a blog post that this update allows developers to streamline real-time feature development by reducing the complexity associated with establishing multiple connections.
The Pub/Sub Editor in the AppSync console has been updated to allow immediate testing of the new WebSocket publishing feature. Developers can select "WebSocket" as the publishing method to trigger a publish_success message upon successful transmission.
The new "publish" WebSocket operation allows clients to publish events to configured channel namespaces after establishing a WebSocket connection. The required message format includes an id, channel, an array of events (up to five), and authorization headers, with each event needing to be a valid JSON string.
AWS provides tools for infrastructure management to further simplify development. The AWS Cloud Development Kit (CDK) offers an infrastructure-as-code framework that eases the configuration and deployment of AppSync Event APIs. The CDK's L2 constructs enable developers to define AppSync Event APIs using familiar programming languages.
Publishing over WebSocket is supported in all regions where AppSync is available, with a client limit of 25 requests per second per connection. For higher rates, the HTTP endpoint remains available, as detailed in the AppSync documentation.
Building Real-Time Apps with AWS AppSync Events’ WebSocket Publishing
Real-time features are essential for modern applications, and AWS AppSync Events provides a fully-managed service for serverless WebSocket APIs. The recent enhancement allows developers to publish messages directly over WebSocket connections, improving the development of real-time features.
To get started with AppSync Events, developers can create an API and automatically receive a default channel namespace and API key. The new WebSocket publishing capability simplifies creating collaborative applications like chat systems and multiplayer games by maintaining a single connection for bi-directional communication.
The message format for publishing events is crucial. Developers need to specify an id, channel, a list of events (up to five), and authorization headers.
For example, a publish message could look like this:
{
"type": "publish",
"id": "an-identifier-for-this-request",
"channel": "/namespace/my/path",
"events": ["{ \"msg\": \"Hello World!\" }"],
"authorization": {
"x-api-key": "da2-12345678901234567890123456-example"
}
}
After publishing, a “publish_success” response will confirm each event sent, or a “publish_error” response if unsuccessful.
Developers can leverage the new AWS Cloud Development Kit (CDK) L2 constructs to configure and deploy an AppSync Event API, making the integration of features like single sign-on (SSO) and multi-factor authentication (MFA) seamless.
Working with AWS AppSync Events: Serverless WebSockets for Pub/Sub
AWS AppSync simplifies connecting applications to events and data. The introduction of AWS AppSync Events allows developers to create real-time experiences using serverless WebSockets. This service is beneficial for applications requiring instant updates without the overhead of traditional API calls.
For example, ephemeral leaderboards can be implemented without persisting data, making them useful in fast-paced environments like gaming or chat applications.
To create an Event API, developers can use the AWS CDK. The following code demonstrates how to set up a basic event API with an API key for authorization:
const cfnEventAPI = new CfnApi(customResources, 'cfnEventAPI', {
name: 'realtime-leaderboard',
eventConfig: {
authProviders: [{ authType: AuthorizationType.API_KEY }],
connectionAuthModes: [{ authType: AuthorizationType.API_KEY }],
defaultPublishAuthModes: [{ authType: AuthorizationType.API_KEY }],
defaultSubscribeAuthModes: [{ authType: AuthorizationType.API_KEY }],
},
});
This setup allows developers to publish events directly to the API, fostering real-time interactions. By integrating features like SSOJet’s secure SSO and user management capabilities, companies can ensure that their applications not only deliver real-time data but also maintain robust security through directory sync, SAML, OIDC, and magic link authentication.
To explore how SSOJet can enhance your application's security while providing seamless user experiences, visit ssojet.com.
Top comments (0)