WebSocket Example
Demonstrates real-time WebSocket support alongside REST APIs.
Source: examples/websocket/
Key Concepts Demonstrated
Section titled “Key Concepts Demonstrated”- Two-phase authentication — JWT sent over encrypted WebSocket, not query params
- Connection lifecycle —
$connect,$disconnect,$defaulthandlers - Connection store —
connectionStorefrom@venturekit/runtime - Send to user — deliver messages to a specific user across sessions
- Broadcast — send to all authenticated connections
- Tenant-scoped messaging —
sendToTenant()for multi-tenancy - Idle timeout — configurable connection timeout
- REST + WebSocket — both APIs in the same project
WebSocket Config
Section titled “WebSocket Config”websocket: { enabled: true, routeSelectionExpression: '$request.body.action', idleTimeoutSec: 600, throttleRateLimit: 100,}Message Format
Section titled “Message Format”{ "action": "auth", "token": "<jwt>" }{ "action": "ping" }{ "action": "broadcast", "data": { "text": "Hello!" } }{ "action": "send", "to": "<userId>", "data": { "text": "Hi!" } }Run It
Section titled “Run It”cd examples/websocketnpm installvk dev
# Connect with wscatwscat -c "wss://<ws-api-id>.execute-api.eu-west-1.amazonaws.com/dev"> {"action": "auth", "token": "<jwt>"}Related
Section titled “Related”- WebSocket Guide
@venturekit/runtime—connectionStore