Skip to content

ManageConnections

Source: src/AWS/ApiGatewayV2/ManageConnections.ts

Runtime binding for the WebSocket @connections management API (execute-api:ManageConnections).

Bind this to a WebSocket API Stage inside a function runtime to push messages to connected clients — the flagship server-push primitive for WebSocket APIs. The binding grants execute-api:ManageConnections scoped to the stage’s @connections ARN and targets the stage’s callback endpoint (https://{apiId}.execute-api.{region}.amazonaws.com/{stage}).

Provide ApiGatewayV2.ManageConnectionsHttp on the hosting function’s Effect (Effect.provide(ApiGatewayV2.ManageConnectionsHttp)) to satisfy the binding.

const connections = yield* ApiGatewayV2.ManageConnections(stage);
yield* ApiGatewayV2.onWebSocketRoute(api, { routeKey: "$default" }, (event) =>
connections
.postToConnection({
ConnectionId: event.requestContext.connectionId,
Data: `echo:${event.body ?? ""}`,
})
.pipe(
Effect.asVoid,
// The peer may have disconnected between send and receive.
Effect.catchTag("GoneException", () => Effect.void),
Effect.orDie,
),
);
yield* connections.deleteConnection({ ConnectionId: staleConnectionId });