FIELD NOTESAWS · ARCHITECTURE

Real-time push on AWS: API Gateway WebSockets or AppSync Events

AppSync Events when channels and fan-out are the whole job; API Gateway WebSockets when each connection needs its own routing and you will run the connection table. Either way a socket lives in one Region, so the client is the failover.

By Space Dog Vin4 min readPublished
ON THIS PAGE
AppSync Events fans a publish out to channel subscribers. With API Gateway WebSockets, a Lambda handler maintains the connection lookup, and Route 53 can point reconnecting clients to a standby API. Drawn in archhy.

If the job is "tell everyone watching this thing that it changed", use AppSync Events: you publish to a channel and AWS keeps the list of who is listening. Choose API Gateway WebSockets only when each connection needs its own routing logic, because then you run the connection table yourself, and that table becomes the most fragile part of the system. Whichever you pick, a connection lives in one Region, so the second Region is a client problem as much as a server one.

Who owns the list of listeners#

Both services give a browser a WebSocket. The difference is who remembers which sockets exist.

With API Gateway, nobody does unless you do. A client connects, the $connect route runs your code, and you write the connection ID somewhere, usually DynamoDB. To push a message, your backend looks up the IDs it wants and calls the @connections management API once per connection, signed with IAM. When the client goes away, $disconnect should delete the row, and sometimes it never arrives, so you also delete rows whenever a post comes back with GoneException.

With AppSync Events, the unit is a channel such as /scores/match-42, grouped under a namespace that carries the auth rules and optional handlers. Clients subscribe, wildcards included (/scores/*), and anything that publishes to the channel reaches all of them. There is no connection ID in your code at all.

JSON
{
  "channel": "/scores/match-42",
  "events": ["{\"home\":2,\"away\":1}"]
}

Send this JSON body in an authenticated HTTP POST to the AppSync Events endpoint. WebSocket publishes also include a unique message ID, a publish type and authorisation fields. Either transport accepts up to five events in one publish.

When the connection table earns its keep#

Run your own table when the routing decision depends on state that only your application has. A trading screen that shows each user the orders they are entitled to, a multiplayer game where the server decides who sees which move, a support desk where messages go to whichever agent claimed the ticket: in each, "who gets this?" is a query against your data, not a channel name. Route selection also lets a client's message pick a backend (sendMove to one function, chat to another), which matters when clients talk back.

Know what you sign up for. A broadcast to ten thousand watchers is ten thousand signed POST calls made by your code, each of which can fail on its own. API Gateway enforces a two-hour connection lifetime and a ten-minute idle timeout, so clients need a heartbeat and a reconnect loop. Frames are limited to 32 KB, so a message larger than that, up to the 128 KB ceiling, has to be split across several.

AppSync Events handlers cover much of the middle ground. onSubscribe can reject a client that asks for a channel it should not see, and onPublish can filter or reshape an event before fan-out. If your per-user logic fits into "may this user join this channel?", channels win. If it is "compute a different payload for each user", you are back to owning connections.

Plan the second Region on day one#

A WebSocket is a long-lived TCP connection to one Regional endpoint. When that Region fails, every open socket drops, and nothing on the server side can move it. AWS's multi-Region disaster recovery post builds the recovery around that fact: a canary keeps opening test connections, a CloudWatch alarm feeds a Route 53 health check, failover DNS sends new connections to the standby Region, and DynamoDB global tables carry the application state there.

Three consequences follow:

  • The client is the failover mechanism. It notices missing heartbeats, reconnects with backoff and jitter to the same custom domain, and asks for its state again.
  • Key enduring application state by user. Keep connection IDs in a separate, transient lookup that the client rebuilds on reconnect.
  • With DynamoDB’s multi-Region eventual consistency, replication is asynchronous. Recent writes may be missing from the standby. Make a slightly stale "give me my state" answer safe.

An Event API is Regional too. Its advantage is that the client rebuilds only its subscriptions, while an API Gateway client must also be re-registered in your table.

On Google Cloud and Azure#

Azure's closest match is Web PubSub: a managed service with connections, users and groups, so you can send to a group (AppSync's channel) or to one user's connections without storing IDs, and its Premium tier adds geo-replication across Regions. Google Cloud has no managed channel service of this kind; Firestore's real-time listeners push document changes, not arbitrary events. The usual build is a Cloud Run service holding the sockets, whose request timeout tops out at 60 minutes, with Memorystore for Redis pub/sub or Firestore real-time listeners keeping instances in step.

For where push fits among queues, topics and buses, see event-driven architecture on AWS; the events you push usually come off a queue, so idempotent consumers applies to whatever feeds the publisher, and messaging across the three clouds covers the layer underneath.

The diagram#

The two designs side by side: a publisher fanning out through an AppSync Events channel, and a Lambda function reading a DynamoDB connection table to post to each API Gateway connection, with Route 53 failover to a standby Region beside it. Open it in archhy and make a copy to adapt it.

Reference documentation: AppSync HTTP publish, AppSync WebSocket publish, API Gateway WebSocket quotas, DynamoDB global tables, Web PubSub geo-replication, Cloud Run WebSockets.