dialWebSocket() function

Available since @irtio/client 5.0BrowsersNode

The dialWebSocket() function opens a WebSocket to the room at a room URL and resolves with a transport once it’s open. Passed as the dial of a DevConnectInit to connect(), or called from a dial of your own that wraps it, it replaces the client’s transport. It’s exported by @irtio/client/dev, with the Dial and Transport types.

Syntax

dialWebSocket(room)

Parameters

room
A string: the room URL, as the client passes it to a dial. An http: or https: URL is dialled as ws: or wss:.

Return value

A Promise that resolves with a transport, an object with these properties:

kind
"websocket". A transport says what carries it here, "webtransport" or "websocket", and the client shows it as transportKind. A transport may leave it out, and then transportKind is undefined.
send(data, unreliable)
Sends one protocol message, a Uint8Array. A WebSocket sends every message reliably, whatever unreliable says.
close(code, reason)
Closes the socket with a close code and a reason.
onmessage(data)
Called with each protocol message that arrives, a Uint8Array.
onclose(code, reason)
Called once when the socket closes after opening.

Exceptions

Error
Rejects with this if the socket closes before it opens, with a message naming the URL it dialled and the close code.

Description

A dial is a function that takes the room URL and returns a Promise of a transport. The client calls it on every connect and reconnect. Without a dial, the client tries WebTransport first and then dialWebSocket().

DevConnectInit is connect()’s init object with development hooks, dial among them. Type the init object as one, since connect()’s parameter is typed as a plain ConnectInit, which has no dial.

The client sets the transport’s onmessage and onclose after the dial resolves. To see incoming data or closes, return a transport of your own whose handlers the client sets, and call them from the inner transport’s. Keep the inner transport’s kind when wrapping it.

Examples

Connecting over WebSocket only

On a network known to block UDP, the client skips WebTransport.

// websocket-only.ts
import { connect } from "@irtio/client";
import { dialWebSocket, type DevConnectInit } from "@irtio/client/dev";

const init: DevConnectInit = { dial: dialWebSocket };
const room = await connect("arena", init);
console.log(room.transportKind); // "websocket"

Logging every close the socket sees

The dial wraps the WebSocket transport in one of its own, so it sees each close, those the client reconnects after included.

// log-closes.ts
import { connect } from "@irtio/client";
import { dialWebSocket, type Dial, type DevConnectInit, type Transport } from "@irtio/client/dev";

const loggingDial: Dial = async (url) => {
  const inner = await dialWebSocket(url);
  const outer: Transport = {
    kind: inner.kind,
    send: (data, unreliable) => inner.send(data, unreliable),
    close: (code, reason) => inner.close(code, reason),
    onmessage: () => {},
    onclose: () => {},
  };
  inner.onmessage = (data) => outer.onmessage(data);
  inner.onclose = (code, reason) => {
    console.warn(`socket closed with ${code} ${reason}`);
    outer.onclose(code, reason);
  };
  return outer;
};

const init: DevConnectInit = { dial: loggingDial };
await connect("arena", init);

The inner handlers call the outer ones when the message or close arrives, by which time the client has set them.

See also

Runtime availability

BrowsersNode
dialWebSocket()5.05.0