WebSocket
Receive WhatsApp messages in real time over a persistent WebSocket connection.
PipesBot streams WhatsApp events to your application over WebSocket connections. No public URL required.
Connect with your App key (ak_) to receive events for every BYON number in your App, in parallel with your App webhook. Sandbox numbers use your account key (pk_) and their own connection scopes, described in Sandbox connections.
How it works
- Connect to the WebSocket endpoint with your App key (
ak_) - Receive a
connectedconfirmation - On API v2, each event arrives as an
eventframe carrying the same envelope as the webhook (legacy Apps receivewhatsapp_messageframes) - Send messages through the same connection with
sendframes, answered bysend_result(see Sending via WebSocket on API v2) - If you disconnect, events are queued per App and delivered when you reconnect (see Offline Queuing)
Connecting
App-scoped connection (BYON ak_ key)
Authenticate with an App's ak_ key to receive messages for all the App's BYON numbers:
wss://api.pipes.bot/ws?apiKey=ak_your_app_keyWelcome message:
{
"type": "connected",
"userId": "user_abc123",
"connectionId": "550e8400-e29b-41d4-a716-446655440000",
"scope": "app",
"authType": "app",
"appId": "app_abc123"
}Authentication
Two key types can open a connection:
- User keys (
pk_+ 32 chars): the first 11 characters are a lookup prefix; the full key is verified against a bcrypt hash. Connections getscope: "user"(orpoolNumber/conversationwhen filtered) andauthType: "user". - App keys (
ak_, BYON): open an app-scoped connection. These getscope: "app"andauthType: "app", are tracked perappId, receive BYON messages for that App, and drain app-scoped offline queues on connect (see Offline Queuing).
Invalid or missing keys receive an HTTP 401 Unauthorized before the WebSocket upgrade completes.
Invalid poolNumberId values (not found, not owned, or not activated) receive an HTTP 403 Forbidden before the upgrade completes.
Invalid conversationId values (not found or not owned) receive an HTTP 403 Forbidden before the upgrade completes.
Keepalive
Send WebSocket ping frames at regular intervals (25 seconds recommended) to keep the connection alive through proxies and load balancers. The server responds with pong automatically.
Reconnection
When the connection drops, reconnect with exponential backoff:
| Attempt | Delay |
|---|---|
| 1 | 1 second |
| 2 | 2 seconds |
| 3 | 4 seconds |
| 4 | 8 seconds |
| ... | ... |
| Max | 30 seconds |
Reset the backoff counter after a successful connection.
Quick example
App-scoped connection (API v2)
import WebSocket from "ws";
const APP_KEY = "ak_your_app_key";
const ws = new WebSocket(`wss://api.pipes.bot/ws?apiKey=${APP_KEY}`);
ws.on("message", (data) => {
const frame = JSON.parse(data.toString());
switch (frame.type) {
case "connected":
console.log("Connected to App", frame.appId); // scope: "app"
break;
case "event": {
const envelope = frame.data; // same envelope as the webhook
if (envelope.field !== "messages") break;
for (const message of envelope.whatsapp.messages ?? []) {
if (message.type !== "text") continue;
// Reply on the number the message arrived on
ws.send(JSON.stringify({
type: "send",
requestId: message.id,
poolNumberId: envelope.pipes.poolNumberId,
message: {
messaging_product: "whatsapp",
// Users who hide their phone number only have a BSUID: reply with `recipient`
...(message.from ? { to: message.from } : { recipient: message.from_user_id }),
type: "text",
text: { body: "Thanks for your message!" },
},
}));
}
break;
}
case "send_result":
if (frame.success) console.log("Sent:", frame.messageId);
else console.error("Send failed:", frame);
break;
}
});Choosing between WebSocket and Webhook
| WebSocket | Webhook | |
|---|---|---|
| Connection | Persistent, bidirectional | Stateless HTTP POST |
| Best for | Real-time apps, bots with instant replies | Serverless functions, background processing |
| Requires | Long-lived process | Publicly accessible URL |
| Offline handling | Messages queued until reconnect | 3 attempts, then kept for manual retry |
Sandbox connections
Sandbox numbers authenticate with your account key (pk_). These connections receive whatsapp_message frames in the legacy format and can be scoped to one sandbox number or one conversation.
User-scoped connection (all messages)
Open a WebSocket connection to receive all messages from all your activated sandbox numbers:
wss://api.pipes.bot/ws?apiKey=YOUR_API_KEYWelcome message:
{
"type": "connected",
"userId": "user_abc123",
"connectionId": "550e8400-e29b-41d4-a716-446655440000",
"scope": "user",
"authType": "user"
}Sandbox-number-scoped connection (filtered by sandbox number)
Open a WebSocket connection to receive only messages from a specific sandbox number:
wss://api.pipes.bot/ws?apiKey=YOUR_API_KEY&poolNumberId=POOL_IDWelcome message:
{
"type": "connected",
"userId": "user_abc123",
"connectionId": "550e8400-e29b-41d4-a716-446655440000",
"scope": "poolNumber",
"authType": "user",
"poolNumberId": "pool_123"
}Conversation-scoped connection (filtered by conversation)
Open a WebSocket connection to receive only messages for a specific conversation:
wss://api.pipes.bot/ws?apiKey=YOUR_API_KEY&conversationId=CONV_IDWelcome message:
{
"type": "connected",
"userId": "user_abc123",
"connectionId": "550e8400-e29b-41d4-a716-446655440000",
"scope": "conversation",
"authType": "user",
"conversationId": "conv_xyz789"
}Connection scoping
User-scoped connections (no conversationId parameter):
- Receive all messages from all activated sandbox numbers
- Queue drain happens on connect: all queued messages are delivered
- Can reply to any owned conversation
Sandbox-number-scoped connections (with poolNumberId parameter):
- Receive only messages from the specified sandbox number
- No queue drain on connect (to avoid partial queue complexity)
- Can only reply to conversations belonging to that sandbox number
Conversation-scoped connections (with conversationId parameter):
- Receive only messages matching the specified conversationId
- No queue drain on connect (to avoid partial queue complexity)
- Can only reply to the scoped conversationId (replies to other conversations are rejected)
Combined scoping (with both conversationId and poolNumberId):
- You can pass both
conversationIdandpoolNumberIdtogether. The server validates that the conversation belongs to the specified sandbox number - Receive only messages matching that specific conversation on that sandbox number
Multiple connections:
- You can open multiple connections simultaneously for the same account
- Messages are broadcast to ALL matching connections:
- User-scoped connections always receive the message
- Sandbox-number-scoped connections receive it only if poolNumberId matches
- Conversation-scoped connections receive it only if conversationId matches
- Use cases: dashboard with per-conversation chat panels, per-number bots, multiple bots on different conversations, one firehose + specific monitors
Sandbox quick examples
User-scoped connection (all messages)
import WebSocket from "ws";
const API_KEY = "pk_your_api_key_here";
const ws = new WebSocket(`wss://api.pipes.bot/ws?apiKey=${API_KEY}`);
ws.on("open", () => {
console.log("Connected to PipesBot");
});
ws.on("message", (data) => {
const event = JSON.parse(data.toString());
switch (event.type) {
case "connected":
console.log("Authenticated as", event.userId);
console.log("Scope:", event.scope); // "user"
break;
case "whatsapp_message":
console.log("Message from", event.data.fromNumber);
console.log("Conversation:", event.data.conversationId);
console.log("Type:", event.data.type);
console.log("Text:", event.data.text);
// Send a reply
ws.send(JSON.stringify({
type: "whatsapp_reply",
data: {
conversationId: event.data.conversationId,
poolNumberId: event.data.poolNumberId,
toNumber: event.data.fromNumber,
text: "Thanks for your message!",
},
}));
break;
case "reply_status":
if (event.data.success) {
console.log("Reply sent:", event.data.messageId);
} else {
console.error("Reply failed:", event.data.error);
}
break;
}
});Sandbox-number-scoped connection (filtered by sandbox number)
import WebSocket from "ws";
const API_KEY = "pk_your_api_key_here";
const POOL_NUMBER_ID = "pool_123";
const ws = new WebSocket(`wss://api.pipes.bot/ws?apiKey=${API_KEY}&poolNumberId=${POOL_NUMBER_ID}`);
ws.on("message", (data) => {
const event = JSON.parse(data.toString());
switch (event.type) {
case "connected":
console.log("Connected, scope:", event.scope); // "poolNumber"
console.log("Scoped to sandbox number:", event.poolNumberId); // "pool_123"
break;
case "whatsapp_message":
// Only messages for pool_123 arrive here
console.log("Message from", event.data.fromNumber);
console.log("Conversation:", event.data.conversationId);
console.log("Text:", event.data.text);
// Reply (must be a conversation belonging to pool_123)
ws.send(JSON.stringify({
type: "whatsapp_reply",
data: {
conversationId: event.data.conversationId,
poolNumberId: event.data.poolNumberId,
toNumber: event.data.fromNumber,
text: "Reply from this sandbox number's bot",
},
}));
break;
}
});Conversation-scoped connection (filtered by conversation)
import WebSocket from "ws";
const API_KEY = "pk_your_api_key_here";
const CONVERSATION_ID = "conv_xyz789";
const ws = new WebSocket(`wss://api.pipes.bot/ws?apiKey=${API_KEY}&conversationId=${CONVERSATION_ID}`);
ws.on("message", (data) => {
const event = JSON.parse(data.toString());
switch (event.type) {
case "connected":
console.log("Connected, scope:", event.scope); // "conversation"
console.log("Scoped to:", event.conversationId); // "conv_xyz789"
break;
case "whatsapp_message":
// Only messages for conv_xyz789 arrive here
console.log("Message from", event.data.fromNumber);
console.log("Text:", event.data.text);
// Reply (must match the connection's conversationId)
ws.send(JSON.stringify({
type: "whatsapp_reply",
data: {
conversationId: event.data.conversationId, // Must be conv_xyz789
poolNumberId: event.data.poolNumberId,
toNumber: event.data.fromNumber,
text: "Reply to this specific conversation",
},
}));
break;
}
});