apigw-websocket-chat
Source on GitHub · All examples
An API Gateway WebSocket API backed by a Node.js Lambda that echoes each message back to the sender.
What it shows
- OpenTofu (
main.tf) creates a WebSocket APIchat-api(route_selection_expression = "$request.body.action"), anodejs20.xLambdachat-handler, anAWS_PROXYintegration, and the$connect,$disconnectand$defaultroutes. - A stage
prodwithauto_deploy. - On
$connectand$disconnectthe Lambda logs and returns 200. On any other message it callsPostToConnection(API Gateway Management API) atAWS_ENDPOINT_URLand sendsEcho: <body>back.
Prerequisites
- Docker, OpenTofu (
tofu),wscat(npm install -g wscat).
WARNING
The compose file mounts /var/run/docker.sock and runs Gopherstack as root so it can start the Lambda container.
NOTE
main.tf points the AWS provider at http://localhost:8000, so Gopherstack must listen there.
Run
docker compose up -d --build
docker compose ps # wait until healthy
./demo.shdemo.sh runs tofu init and tofu apply, then prints the wscat command. It does not open a connection itself.
On Docker Desktop, start the stack with LAMBDA_DOCKER_HOST=host.docker.internal docker compose up -d --build. The default is 172.17.0.1, the Linux Docker bridge gateway.
Expected output
websocket_url = "https://<api-id>.execute-api.us-east-1.amazonaws.com/prod"
=== Done ===
The WebSocket API has been provisioned. To test it, use wscat:
wscat -c ws://localhost:8000/v2proxy/<api-id>/prod
Then send any text; the Lambda echoes it back as 'Echo: <text>'.The websocket_url output is the AWS-style host and is not reachable. Connect with the ws://localhost:8000/v2proxy/<api-id>/prod URL the script prints:
$ wscat -c ws://localhost:8000/v2proxy/<api-id>/prod
Connected (press CTRL+C to quit)
> {"action":"msg","text":"hi"}
< Echo: {"action":"msg","text":"hi"}Files
| File | Purpose |
|---|---|
main.tf | API, routes, stage, Lambda, IAM role |
src/index.js | Handler for $connect, $disconnect and the echo |
demo.sh | tofu init, tofu apply, prints the wscat URL |
Clean up
tofu destroy -auto-approve
docker compose down -v