Skip to main content
INS // Insights

Custom AWS API Gateway WebSocket Build

Updated October 2026 · 8 min read

A custom AWS API Gateway WebSocket build is the live channel we ship when a browser needs server push and the message has to stay inside one tenant. API Gateway terminates the socket. A $connect integration writes the connection id and the tenant into DynamoDB. A publisher queries that tenant and calls PostToConnection for each id. A $disconnect path and a 410 from the management API remove rows that are already gone. The table is the directory. The tenant key is the boundary.

Rutagon builds this when a GraphQL subscription is the wrong shape for the client. The AppSync build is that other path. API Gateway patterns cover HTTP edges. This one is the WebSocket API, the connection table, and tenant-scoped fan-out. AWS cloud infrastructure is the surrounding account design. Quotas we size against are on the WebSocket limits page: a 2-hour maximum connection, a 10-minute idle timeout, a 32 KB frame, a 128 KB message, and a default of 500 new connections per second per account per Region across WebSocket APIs.

A custom AWS API Gateway WebSocket build, connection then fan-out

$connect is the only place authorization runs. API Gateway invokes it while the upgrade is still pending. If the authorizer or the integration fails, the socket is not established, and the client sees 401 or 403. We do not write the DynamoDB row after returning success from a different path. The row is written in $connect, or the connection is not ours to use. The connection id comes from the request context. AWS documents it as a variable-length string whose format can change. We store it as an opaque key. We do not parse it.

The tenant id is taken from the authorizer context, which was derived from a token checked at connect. It is not taken from a later message body. A client that asks to "also subscribe to another tenant" is asking the publisher to ignore the row we stored. The publisher does not offer that method. Fan-out input is a tenant id the server already trusts, plus a payload. The query is tenant_id = :tenant. There is no scan of the table.

table: pk tenant_id, sk connection_id
attrs: principal, connected_at, api_id, stage, ttl
connect: authorizer sets tenant, PutItem, then 200
disconnect: DeleteItem, best effort
publish: Query tenant_id, PostToConnection each sk
on 410: DeleteItem that connection_id
payload: under 32 KB so it fits in one frame

$disconnect runs after the socket is already closed. Delivery is best effort. A laptop that vanishes can leave a row. The publisher treats HTTP 410 from PostToConnection as the delete. The next fan-out for that tenant does not call a dead id twice. DynamoDB TTL is only a backstop, set from connected_at plus the 2-hour maximum plus a buffer. TTL deletion is not prompt enough to keep a fan-out clean the same hour. The 410 is.

We do not call PostToConnection from the $connect function. The upgrade is not finished while that function runs, and a post in that window fails. If the product needs a greeting frame, a second worker sends it after $connect has returned and a GetConnection confirms the id. The greeting is optional. The row write is not.

Idle timeout is 10 minutes and is not a quota you can raise. Clients send a ping inside that window. The ping hits $default or a ping route and returns without a DynamoDB write and without a fan-out. A ping that republishes to the tenant wakes every socket for a heartbeat one client sent. Connection duration is 2 hours and is not a quota you can raise. Clients reconnect before that, get a new connection id, and $connect writes a new row. The old id disappears on $disconnect or on the next 410. Anything that cached connection ids outside the table is wrong after the first reconnect.

The frame limit is 32 KB. A message can be 128 KB if it is split into frames, and a larger frame closes the socket with 1009. Our publisher keeps the payload under 32 KB so fan-out does not depend on fragmentation. A larger state snapshot goes through HTTP, and the socket carries a pointer. Integration timeout caps at 29 seconds. $connect does the token check and one PutItem inside that. It does not call a downstream system that can stall the upgrade. A slow $connect is a client that never opens.

New connections default to 500 per second per account per Region, shared by every WebSocket API in that account. A deploy that drops everyone at once reconnects into that quota. We alarm on throttled connect attempts. We do not put the WebSocket API in the same account and Region as another product's sockets unless sharing 500 new connections per second is a choice we wrote down. The limit can be raised. The design does not assume it already was.

Tenant scope and the roles that enforce it

The publisher role can dynamodb:Query on the table and execute-api:ManageConnections on POST /@connections/* for this API and stage. It cannot PutItem a connection for a tenant it does not serve, because it cannot PutItem at all. The $connect role can PutItem and cannot call ManageConnections. The $disconnect role can DeleteItem and nothing else. The authorizer role can read the key material it needs to check the token and cannot touch the table. A stolen publisher role can push frames to connections that already exist. It cannot invent a connection row for a tenant that has nobody online, and it cannot read the token store. A stolen connect role can write a row and cannot emit frames.

The query returns only the tenant stored at connect. The publisher role has no scan permission, so a fan-out cannot walk every connection and filter in memory. Lambda and DynamoDB APIs are the request-response cousin of this design. The WebSocket build adds the long-lived id. DeleteItem on 410 is idempotent, and we page on connect throttles at the account quota rather than on a single gone socket.

What we test before it is done

A rejected token does not write a row, and the socket does not open. A valid token writes one item whose tenant_id matches the authorizer, not a tenant id supplied beside the token. A publish to tenant A calls PostToConnection only for A's connection ids. Tenant B's sockets stay quiet, including when the payload names tenant B. A 410 deletes the item, and a second publish does not call that id. A client idle past 10 minutes drops, and the following publish cleans the row if $disconnect never arrived. A reconnect during the 2-hour cap creates a new id and a new row. A payload over 32 KB is rejected in the publisher before the management API closes the socket.

We leave the route set small: $connect, $disconnect, ping, and one application route if the client must send a command. Commands that mutate state still go through the HTTP API, with the same authorizer's identity, and the socket is the notification that the command landed. Putting the mutation on the socket makes $default a second backend with worse timeout and retry behavior. The HTTP API keeps retries and idempotency keys. The socket tells the tenant's live browsers that the record changed.

Rutagon ships the API, the table, the three roles, the publisher, and the tests above. The 2-hour cap, the 10-minute idle timeout, the 32 KB frame, and the 500 new connections per second are the quotas the client and the account have to live with. The tenant key is the part we will not loosen to make a demo easier.

Frequently Asked Questions

Why is the tenant stored at connect instead of on each message?

Authorization runs on $connect only. The authorizer's tenant is written on the item. Later messages are not a second login. A publisher that trusted a tenant id from the payload would let one socket pull another tenant's fan-out. The query key is the value stored at connect.

What removes a connection the client did not close cleanly?

$disconnect is best effort and may never run. PostToConnection returns 410 when the id is already gone, and the publisher deletes that item. TTL on the row is a backstop around the 2-hour maximum connection duration. Fan-out correctness comes from the 410, not from waiting for TTL.

Why keep payloads under 32 KB?

The WebSocket frame quota is 32 KB, and a larger frame closes the connection with 1009. Message payload can reach 128 KB only as multiple frames. The publisher sends a single frame under 32 KB, or it sends a pointer and lets HTTP carry the snapshot. The greeting path is a separate worker, because $connect must finish before any post.

How are the IAM roles split?

The connect role can put a connection item and cannot manage connections. The disconnect role can delete an item. The publisher role can query by tenant and call PostToConnection on this API stage, and it cannot write connection rows. The authorizer does not receive those permissions. A stolen publisher can notify live sockets. It cannot mint a tenant membership.

Discuss this WebSocket build for a tenant-scoped live feed. Contact Rutagon at rutagon.com/contact, 907-841-8407, or contact@rutagon.com.