peerix - v0.8.0
    Preparing search index...

    Class SocketIoDriver

    Socket.IO-based signaling driver.

    This driver uses Socket.IO to relay signaling messages between clients via your own WebSocket server.

    Expected Socket.IO events, where prefix is prepended verbatim (include a separator such as : in it; it is empty by default):

    • Client -> Server: prefix + subscribe, unsubscribe, publish
    • Server -> Client: prefix + message

    A namespace that fails to subscribe is retried with exponential backoff while the driver stays active, and rooms announce themselves again once it is restored.

    This is the typical signaling flow:

    %%{init:{"theme":"dark"}}%% sequenceDiagram participant A as Peer A participant S as Socket.IO Server participant B as Peer B A->>S: peerix:subscribe(namespace) B->>S: peerix:subscribe(namespace) A->>S: peerix:publish(namespace, payload) S->>B: peerix:message(namespace, payload) B->>S: peerix:unsubscribe(namespace)
    %%{init:{"theme":"default"}}%% sequenceDiagram participant A as Peer A participant S as Socket.IO Server participant B as Peer B A->>S: peerix:subscribe(namespace) B->>S: peerix:subscribe(namespace) A->>S: peerix:publish(namespace, payload) S->>B: peerix:message(namespace, payload) B->>S: peerix:unsubscribe(namespace)
    sequenceDiagram
        participant A as Peer A
        participant S as Socket.IO Server
        participant B as Peer B
        A->>S: peerix:subscribe(namespace)
        B->>S: peerix:subscribe(namespace)
        A->>S: peerix:publish(namespace, payload)
        S->>B: peerix:message(namespace, payload)
        B->>S: peerix:unsubscribe(namespace)

    This driver requires the socket.io-client module in the browser and the socket.io module for server-side use in Node.js.

    Client-side code (browser with Socket.IO client):

    import { io } from "socket.io-client";

    // connect to a Socket.IO server (e.g. at localhost:8080)
    const socket = io("http://localhost:8080");

    // create a new driver instance
    const driver = new SocketIoDriver({ socket, prefix: "peerix:" });

    Server-side code (Node.js with Socket.IO):

    const { Server } = require("socket.io");
    const io = new Server(8080, { cors: { origin: "*" } });

    io.on("connection", (socket) => {
    socket.on("peerix:subscribe", (namespace, callback) => {
    socket.join(namespace);
    if (callback) callback();
    });

    socket.on("peerix:unsubscribe", (namespace, callback) => {
    socket.leave(namespace);
    if (callback) callback();
    });

    socket.on("peerix:publish", (namespace, data, callback) => {
    socket.broadcast.to(namespace).emit("peerix:message", namespace, data);
    if (callback) callback();
    });
    });

    Hierarchy (View Summary)

    Index
    • Creates a new instance of the driver.

      Parameters

      • options: { ackTimeout?: number; prefix?: string; socket: SocketIoClient }

        Configuration options for the driver.

        • OptionalackTimeout?: number

          Optional timeout for acknowledgements in milliseconds (default: 10000). A value of 0 or less waits for acknowledgements indefinitely.

        • Optionalprefix?: string

          Optional prefix prepended verbatim to event names (for example "peerix:").

        • socket: SocketIoClient

          Socket.IO socket instance.

      Returns SocketIoDriver

      If socket is not a valid Socket.IO socket.

    • get active(): boolean

      Indicates whether the driver is currently active. Subclasses must keep it in sync with their connection: signaling messages are only published while the driver is active, and joined rooms announce themselves again when it becomes active. A namespace that fails on its own leaves the driver active, and emitting active once it is subscribed again makes rooms announce themselves to the peers they missed.

      Returns boolean

    • set active(value: boolean): void

      Sets the active state of the driver and emits corresponding events.

      Parameters

      • value: boolean

      Returns void

    • Destroys the driver instance, cleaning up any resources. Emits inactive if the driver was active, then removes all event handlers.

      Returns void

    • Registers an event handler for the specified internal event.

      Type Parameters

      Parameters

      • event: K

        The event name.

      • handler: (...args: DriverEvents[K]) => void

        The event handler function.

      • Optionaloptions: { signal?: AbortSignal }

        Listener options.

        • Optionalsignal?: AbortSignal

          Removes the handler when aborted.

      Returns () => void

      A function that removes the handler.

    • Publishes a signaling message to the specified namespace.

      Parameters

      • namespace: string

        The namespace to publish the message to.

      • data: number[]

        The message data to publish.

      Returns Promise<void>

      Nothing directly; resolves once the message is delivered (async drivers may return a promise).

    • Subscribes to signaling messages for the specified namespace.

      Parameters

      • namespace: string

        The namespace to subscribe to.

      • handler: (data: number[]) => void

        The handler function to call when a message is received.

      Returns Promise<void>

      Nothing directly; resolves once the subscription is established (async drivers may return a promise).

    • Unsubscribes from signaling messages for the specified namespace.

      Parameters

      • namespace: string

        The namespace to unsubscribe from.

      • handler: (data: number[]) => void

        The handler function to remove.

      Returns Promise<void>

      Nothing directly; resolves once the unsubscription is confirmed (async drivers may return a promise).