Skip to content

WebSocketEventSource

Source: src/AWS/Lambda/WebSocketEventSource.ts

Connects a WebSocket API route to the current Lambda function.

At deploy time this layer materializes the AWS_PROXY Integration, the Route, and the API Gateway invoke Permission for each registered route key; at runtime it dispatches matching WebSocket proxy events to the registered handler.

const connections = yield* AWS.ApiGatewayV2.ManageConnections(stage);
yield* AWS.ApiGatewayV2.onWebSocketRoute(api, { routeKey: "$connect" }, () =>
Effect.succeed({ statusCode: 200 }),
);
yield* AWS.ApiGatewayV2.onWebSocketRoute(api, { routeKey: "$default" }, (event) =>
connections
.postToConnection({
ConnectionId: event.requestContext.connectionId,
Data: `echo:${event.body ?? ""}`,
})
.pipe(
Effect.asVoid,
Effect.catchTag("GoneException", () => Effect.void),
Effect.orDie,
),
);