Building Real-Time APIs with WebSockets and Publish-Subscribe
This lesson explains how API Gateway WebSocket APIs keep persistent connections, how back ends push messages, and how publish-subscribe decouples producers from connections.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the WebSocket API routes and how connection IDs are used.
- Explain the connection limits that clients and servers must design for.
- Design a publish-subscribe fan-out to subscribed connections.
API Gateway WebSocket APIs
A WebSocket API keeps a persistent, bidirectional connection with each client. Messages are routed by a route selection expression (for example on an action field) to integrations such as Lambda. Three special routes manage the connection: $connect authorises the client (with a Lambda or IAM authoriser) and is where the application records the new connection ID, $disconnect cleans up, and $default handles unmatched messages. To send data to a client, back-end code calls the @connections management API with the connection ID.
Connections are closed after 10 minutes without messages and after a maximum of two hours, so clients send keepalive messages and reconnect automatically, and the server must not rely on connections living forever. Account quotas include the rate of new connections per second, which large planned events may need to raise in advance.
Publish-subscribe behind the connections
The connection layer should not be coupled to every producer. Producers publish events to EventBridge, SNS or Kinesis; a consumer looks up which connections subscribe to the event, for example in a DynamoDB table keyed by topic or room with the connection IDs, and posts the message to each. Stale connections return HTTP 410 (Gone) and are removed from the table, and a TTL attribute removes entries that were never cleaned up.
-
Connect
The client connects; $connect authorises it and stores the connection ID with its subscriptions.
-
Publish
A producer publishes an event to a topic or event bus without knowing about connections.
-
Fan out
A consumer reads the subscribers for the event and calls @connections for each.
-
Clean up
$disconnect and 410 responses remove connection records.
The following diagram shows a score change reaching the fans who follow that match.

Pushing an update to subscribers
Design considerations
- Store subscriptions server-side, because clients reconnect with new connection IDs.
- Use Lambda concurrency limits and batching in the fan-out consumer for very large audiences.
- Authenticate on $connect with tokens, because browsers cannot add custom headers to WebSocket handshakes; pass a token in the query string or use IAM.
- Plan quotas for peak connections and connection rate, and load test before large events.
Live sports scores
A sports app must push score changes to up to one million fans within a second, and fans follow specific matches.
Fans connect to a WebSocket API; $connect validates their Cognito token, and a subscribe route stores the connection ID under each match in DynamoDB.
The scoring system publishes score events to EventBridge; a Lambda function queries the subscribers for the match and posts updates in parallel batches, removing connections that return 410. Before the final, the team raises the new-connection quota and load tests the fan-out.
Why must WebSocket subscriptions be stored in a database rather than in the Lambda function's memory?
Show answer
Lambda functions are stateless and short-lived, and clients reconnect with new connection IDs, so the mapping of subscriptions to connections must live in a shared store such as DynamoDB.
Exam Tips
Remember for the exam
- Server must push updates to browsers without polling: API Gateway WebSocket API.
- Track which clients to notify: connection IDs and subscriptions in DynamoDB.
- Clients lose updates after a while: idle timeout (10 minutes) and maximum duration (2 hours); reconnect.
- Decouple producers from connections: publish to EventBridge or SNS and fan out from a consumer.