A sports club wants its fans to see goals, cards and substitutions the moment they happen. The first idea is usually polling: the browser asks the backend every two seconds whether something changed. It works, but most of those requests come back empty, and the updates still lag. What you actually want is for the server to push events to the browser on its own.
On AWS you can do that without running a single server: API Gateway keeps the WebSocket connections open, and Lambda only runs when someone connects, disconnects or sends a message. I built exactly this kind of live ticker with TypeScript, DynamoDB and AWS CDK in WebSocket API with Lambda: a live ticker step by step. Here are the ideas that matter most.
The mental model in three pieces
-
Routes.
$connect,$disconnectand$defaultare predefined. Custom routes are chosen by the route selection expression, typically$request.body.action, so{"action":"ping"}lands in apingroute. -
Connection IDs in your own storage. API Gateway hands out a
connectionIdbut has no idea which match a fan is following. That mapping lives in a DynamoDB table with an index per match. -
The @connections API for pushing. To send something unprompted, your backend calls
PostToConnectionfor each connection ID. There is no built-in broadcast.
Before reaching for WebSockets at all, check whether you really need two-way traffic. If only the server sends, server-sent events are often simpler. And for very large audiences, AppSync Events gives you channels and fan-out out of the box.
The quirks that bite in production
This is where most of the surprises are:
-
$disconnectis best effort. It can simply never arrive. A TTL attribute on the connection table and treating HTTP 410 (GoneException) as a normal outcome keep stale entries from piling up. -
Don't send in
$connect. The connection isn't fully established yet, so a push at that point fails with 410. - Hard limits shape the design: connections end after 2 hours, close after 10 minutes without traffic, and frames are capped at 32 KB. Clients need an application-level ping and an automatic reconnect with a random delay.
- Keep publishing off the socket. If the publish function were a WebSocket route, any fan who knows the URL could report fake goals. It should be invoked separately and protected by IAM.
Fan-out is the heart of it. Sending one by one is too slow for 2,000 fans, firing everything at once invites throttling, so a handful of workers share a queue. Simplified, the per-connection send looks like this:
async function send(id: string, data: string): Promise<'sent' | 'gone'> {
try {
await client.send(new PostToConnectionCommand({
ConnectionId: id,
Data: data,
}));
return 'sent';
} catch (err) {
if (err instanceof GoneException) return 'gone'; // remove from table
throw err;
}
}
Is it worth it?
For tickers, internal dashboards or notifications in business apps, this is my first choice: a few small, testable functions, no load balancer with sticky sessions, nothing to restart at 3 a.m. The weak spot is the fan-out, because every message to every client is its own API call. Beyond a few tens of thousands of receivers per event, I would look at AppSync Events or IoT Core instead.
Cost-wise it stays small. In the article I calculate a match with 2,000 fans connected for two hours: the connection minutes come to about 6 cents and the messages to just under 40 cents, with Lambda and DynamoDB in the cent range.
What the full article covers
- The complete CDK stack in TypeScript, including the DynamoDB table, TTL and the
byMatchindex - The broadcast with limited parallelism, the handlers and local tests with the Node.js test runner
- A Node.js fan client to try everything from three terminals, plus a Serverless Framework variant
- Troubleshooting for 410, 403, idle disconnects and browser authentication, plus the full cost calculation
All the details and runnable code: WebSocket API with Lambda: a live ticker step by step
π This is the condensed version. The full article on sascha-ms.de goes deeper β with all code samples, diagrams and a short knowledge check.
π Next step: Curious what your own Lambda functions cost? Try my Lambda cost calculator β it runs right in your browser.
π¬ Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).
Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en
Top comments (1)
The $disconnect-is-best-effort point is the one that would have bitten me β treating 410 as a normal outcome and letting a TTL clean up stale connections is a much saner model than trying to track disconnects reliably. The publish-off-the-socket rule is the other keeper: a fan who knows the URL reporting fake goals is exactly the kind of thing you'd only discover in production. For the 2,000-fan fan-out, what concurrency did you settle on for the parallel PostToConnection calls? At goal time every fan wants the update at once, so the batch size feels like the number that decides whether the ticker actually feels live.