Skip to main content

Function: websockets()

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

Defined in: packages/core/src/utils/fetch-helpers.ts:453

Create a fetch function that uses WebSocket transport

WebSocket support as a custom fetch implementation. Follows the same composable pattern as other fetch helpers.

Parameters

config

WebSocket configuration

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

Fetch-compatible function that uses WebSocket for supported requests

(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

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