Part 8 of the Serverless series.

Real-time features (live scores, chat, notifications, multiplayer games) need a persistent connection from server to client. Running WebSocket servers yourself means tracking connections, scaling them, and fanning messages out to the right clients. AWS AppSync Events does all of that as a managed service: you publish an event to a channel, and every client subscribed to it receives the event.

No GraphQL schema is needed. It’s pure publish and subscribe.

Why I switched to it

My first multiplayer game, QuizDash, used an API Gateway WebSocket API. It worked, but I had to store every connection ID in DynamoDB, look up who was in each game, and loop over connections to send each message, handling stale connections along the way.

In Trivolt, AppSync Events replaces all of that. When the host opens a question, the backend publishes one event to the game’s channel, and AppSync delivers it to every player’s phone and the big screen. You can see how it’s wired up in the Trivolt source code.

The building blocks

  • Event API: the endpoint, with an HTTP domain for publishing and a real-time domain for WebSocket connections.
  • Channel namespaces: top-level groupings such as game, each with its own authorisation rules and handlers.
  • Channels: paths within a namespace, created on the fly simply by using them: /game/ABC123. Subscribers can use wildcards: /game/*.

Publishing and subscribing

Publish from a backend with a signed HTTP request (or over the WebSocket from a client):

POST https://<api-id>.appsync-api.<region>.amazonaws.com/event
{
  "channel": "/game/ABC123",
  "events": ["{\"type\":\"QuestionOpened\",\"questionId\":\"q-07\"}"]
}

Clients subscribe over WebSocket. With the AWS Amplify library:

import { events } from 'aws-amplify/data';

const channel = await events.connect('/game/ABC123');
channel.subscribe({
  next: (event) => render(event),
  error: (err) => console.error(err),
});

Event handlers

Each namespace can run small JavaScript handlers inside AppSync:

  • onPublish can validate, transform or drop events before they’re delivered.
  • onSubscribe can reject subscriptions, for example when a player asks for a channel that isn’t theirs.

Handlers can also call data sources (Lambda, DynamoDB and others), so an event can be persisted or enriched as it passes through.

Authorisation

Like AppSync GraphQL, Event APIs support API keys, Cognito, IAM, OIDC and Lambda authorisers, set per namespace and per operation (connect, publish, subscribe). A common shape, and Trivolt’s:

  • the backend publishes with IAM,
  • signed-in hosts use Cognito, and
  • anonymous players get short-lived tokens that only allow subscribing to their own game’s channel.

Clients can then listen, but never forge “the answer is B” events.

Hands-on

  1. In the AppSync console, choose Create API → Event API. It creates a default namespace and an API key.
  2. Open the Pub/Sub editor, subscribe to /default/test in one panel, and publish to it from another. Then try subscribing to /default/*.
  3. Add an onPublish handler that adds a receivedAt timestamp to every event, and publish again.

When to use it

  • AppSync Events: broadcasting server-side events to many clients by topic. The simplest option for most real-time features.
  • AppSync GraphQL subscriptions: real-time updates tied to GraphQL mutations and data.
  • API Gateway WebSocket APIs: when you need custom routing of client messages to backend integrations, and are happy to manage connections.

Gotchas

  • Delivery is best effort to connected clients. A phone that drops off and reconnects misses events sent in between. Design clients to fetch current state on reconnect, and treat events as “something changed” hints.
  • Keep channel names unguessable or authorised. Anyone allowed to subscribe to /game/* sees every game.
  • Watch connection minutes on the bill for clients that stay connected all day.

Next: Event-driven patterns you can ship to production.

← Series overview