EdgeStream
WebSocket Transport
RawWebSocketTransport provides a direct, low-overhead WebSocket connection. It auto-converts http:// to ws:// and https:// to wss://, supports binary (ArrayBuffer) frames, and reconnects automatically with exponential backoff.
Configuration
stream.registerServer({
id: 'chat',
type: 'chat',
url: 'wss://chat.bizfirst.com/ws',
transportConfig: {
type: 'websocket',
url: 'wss://chat.bizfirst.com/ws', // or https:// — auto-converted to wss://
// Auth via query params (WebSocket cannot send request headers)
queryParams: {
token: authStore.getToken(),
clientId: 'app-client-1',
},
// Connection timeout (default: 10000ms)
timeoutMs: 10000,
// Reconnect configuration
reconnect: {
maxAttempts: 0, // 0 = unlimited
initialDelayMs: 1000,
maxDelayMs: 30000,
backoffMultiplier: 2,
},
}
});
URL Conversion
The transport automatically converts HTTP URLs to WebSocket protocol:
// These all result in a wss:// WebSocket connection
transportConfig: { type: 'websocket', url: 'https://chat.bizfirst.com/ws' }
transportConfig: { type: 'websocket', url: 'wss://chat.bizfirst.com/ws' }
// These result in ws:// (unencrypted — dev only)
transportConfig: { type: 'websocket', url: 'http://localhost:5000/ws' }
transportConfig: { type: 'websocket', url: 'ws://localhost:5000/ws' }
Binary Message Support
The WebSocket transport sets binaryType = 'arraybuffer', so messages arriving as binary frames are passed to the pipeline as ArrayBuffer:
// Receive — binary or text, handled transparently
stream.subscribe('chat', 'agent.*', (envelope) => {
// envelope.raw is string | ArrayBuffer depending on what server sent
console.log('Received:', envelope.body);
});
// Send — pass string or ArrayBuffer
const server = stream.server('chat')!;
await server.transport.send(new Uint8Array([0x01, 0x02, 0x03]).buffer);
await server.transport.send(JSON.stringify({ type: 'ping' }));
Automatic Reconnection
When the connection drops, RawWebSocketTransport automatically retries with exponential backoff:
// Reconnect internals — calculateRetryDelay with exponential backoff
// Attempt 1: 1000ms delay
// Attempt 2: 2000ms delay
// Attempt 3: 4000ms delay
// ...
// Capped at maxDelayMs (default 30000ms)
// maxAttempts: 0 means unlimited retries — the connection never gives up
No SignalR Fallback
Unlike SignalR, RawWebSocketTransport does not fall back to SSE or polling if WebSocket is unavailable. If your environment has WebSocket restrictions (proxies, corporate firewalls), use SignalR instead which handles negotiation automatically.
Query Parameter Authentication
WebSocket connections cannot send custom headers during the initial handshake. Pass auth tokens via query parameters instead:
// Auth via query params — appended to the WebSocket URL
transportConfig: {
type: 'websocket',
url: 'wss://chat.bizfirst.com/ws',
queryParams: {
access_token: authStore.getJwtToken(),
tenant: tenantId,
}
}
// Resulting URL: wss://chat.bizfirst.com/ws?access_token=eyJ...&tenant=acme