Skip to content

@my-swu/simulator-client


@my-swu/simulator-client / WebSocketFactory

Type Alias: WebSocketFactory ​

WebSocketFactory = (url, protocols?) => EngineWebSocket

Defined in: packages/ts-sdk/src/internal/transport.ts:81

Factory used to create WebSocket clients.

Use this in runtimes without a global browser-style WebSocket.

The optional protocols argument carries the WebSocket subprotocol list used to authenticate the connection (the auth token is sent off-URL via Sec-WebSocket-Protocol; see the connection docs). A custom factory MUST forward protocols to its underlying socket, otherwise authentication will fail once tokens are no longer placed in the URL.

Example:

ts
import type { WebSocketFactory } from '@my-swu/simulator-client'

const webSocketFactory: WebSocketFactory = url => new WebSocket(url)

console.log(webSocketFactory)

Parameters ​

url ​

Example:

ts
import type { WebSocketFactory } from '@my-swu/simulator-client'

// The SDK calls this with the resolved match socket URL.
const webSocketFactory: WebSocketFactory = (socketUrl) => new WebSocket(socketUrl)

string

protocols? ​

Example:

ts
import type { WebSocketFactory } from '@my-swu/simulator-client'

// The SDK passes subprotocols when a transport needs them; forward them on.
const webSocketFactory: WebSocketFactory = (socketUrl, socketProtocols) => (
  new WebSocket(socketUrl, socketProtocols)
)

string | string[]

Returns ​

EngineWebSocket

Example ​

ts
import WebSocket from 'ws'
import type { WebSocketFactory } from '@my-swu/simulator-client'

const nodeWebSocketFactory: WebSocketFactory = (url, protocols) => new WebSocket(url, protocols)
console.log(typeof nodeWebSocketFactory)

See ​

https://simulator-sdk.my-swu.com/guide/runtime-adapters

Released under the MIT License.