WebSocket Trigger Block
The WebSocket Trigger block creates a WebSocket API endpoint using AWS API Gateway. It enables real-time, bidirectional communication between clients and your workflow.
AWS Service: Amazon API Gateway WebSocket APIs
Configuration Options
The WebSocket Trigger block currently has no additional configuration options. It creates a WebSocket endpoint that handles connection events.
How WebSockets Work
Unlike HTTP APIs (request-response), WebSocket APIs maintain persistent connections:
Client Server
| |
|------- Connect ($connect) --->|
| |
|<------ Message --------------->|
|<------ Message --------------->|
|<------ Message --------------->|
| |
|------- Disconnect ----------->|
| |
WebSocket Routes
| Route | Description |
|---|---|
$connect |
Triggered when a client establishes a connection |
$disconnect |
Triggered when a client disconnects |
$default |
Handles messages that don't match other routes |
Use Cases
Real-Time Chat
Clients connect and send messages that are broadcast to other connected users.
Live Notifications
Push notifications to connected clients in real-time.
Live Dashboards
Stream live data updates to dashboard clients.
Collaborative Editing
Synchronize document changes between multiple users in real-time.
AI Thinking Feedback
Send intermediate thinking steps from AI models to clients for enhanced user experience.
Message Format
Incoming Messages
Messages from clients are received as JSON in the Function block:
// Example incoming message
{
"ctx": {
"webSocket": {
"routeKey": "$default",
"connectionId": "abc123",
"domainName": "example.com",
"stage": "prod"
},
}
"payload": "Hello, World!"
}
Sending Messages to Clients
To send messages back to connected clients, use the connection ID:
Connection Management
Connection ID
Each client connection has a unique connectionId that identifies it. You can send this id to other blocks, or store it to target specific clients.
AWS Reference
- API Gateway WebSocket APIs
- WebSocket Route Selection
- Managing WebSocket Connections
- WebSocket API Quotas
Quotas and Limits
| Limit | Value |
|---|---|
| Max message size | 128 KB |
| Connection duration | 2 hours (idle timeout: 10 minutes) |
| Max connections per API | 500 (soft limit, can be increased) |
Best Practices
- Store connection IDs: Use DynamoDB to track active connections
- Handle disconnects gracefully: Clean up resources on
$disconnect - Implement heartbeats: Keep connections alive with periodic pings
- Use connection-scoped data: Store user context with connection ID
- Handle errors: Implement retry logic for failed message sends
- Scale appropriately: Consider connection limits for high-traffic applications