Skip to main content

Function: websockets()

websockets(config): {(input, init?): Promise<Response>; (input, init?): Promise<Response>; }

Defined in: packages/core/src/platform/websocket-fetch.ts:482

A fetch that sends ws:///wss:// requests (or any marked X-Transport: websocket) over a pooled WebSocket, through the platform adapter, and passes the rest to baseFetch. The response body streams the messages as JSON lines; a connection failure is a 500 response, not a throw.

Parameters​

config​

baseFetch?​

{(input, init?): Promise<Response>; (input, init?): Promise<Response>; }

connectionUrl​

string

events?​

Record<string, (data) => void>

headers?​

Record<string, string>

reconnect?​

{ backoff?: "linear" | "exponential"; initialDelay?: number; maxAttempts?: number; maxDelay?: number; }

reconnect.backoff?​

"linear" | "exponential"

reconnect.initialDelay?​

number

reconnect.maxAttempts?​

number

reconnect.maxDelay?​

number

Returns​

(input, init?): Promise<Response>

MDN Reference

Parameters​

input​

RequestInfo | URL

init?​

RequestInit

Returns​

Promise<Response>

(input, init?): Promise<Response>

MDN Reference

Parameters​

input​

string | Request | URL

init?​

RequestInit

Returns​

Promise<Response>

Examples​

Simple usage

import { websockets } from '@baleybots/core';

const bot = new Baleybot({
model: {
id: 'gpt-4o-realtime-preview',
provider: {
fetch: websockets({
connectionUrl: 'wss://api.openai.com/v1/realtime'
})
}
}
});

With reconnection

websockets({
connectionUrl: 'wss://api.example.com',
reconnect: { maxAttempts: 5 }
})

With event handlers

websockets({
connectionUrl: 'wss://api.example.com',
events: {
'custom.event': (data) => { console.log(data); }
},
reconnect: { maxAttempts: 5, backoff: 'exponential' }
})