peerix - v0.8.0
    Preparing search index...

    Class NatsDriver

    NATS-based signaling driver.

    This driver uses NATS as the underlying messaging system, enabling distributed signaling across multiple browsers and devices.

    A subscription the server ends, such as one a permissions violation closes, is retried with exponential backoff, and rooms announce themselves again once it is restored.

    For testing, you can use the public server at wss://demo.nats.io:8443.

    This driver requires the @nats-io/nats-core module for WebSocket-based NATS connections directly in the browser.

    import { wsconnect } from "@nats-io/nats-core";

    // connect to a NATS server (e.g., the local server)
    const nc = await wsconnect({ servers: ["ws://localhost:8080"], noEcho: true });

    // create a new driver instance
    const driver = new NatsDriver({ nc });

    Running a local NATS server with WebSocket support for testing:

    cat << EOF > /tmp/nats-server.conf
    websocket: {
    port: 8080,
    no_tls: true,
    same_origin: false
    }
    EOF

    podman run --rm -p 4222:4222 -p 8080:8080 \
    -v /tmp/nats-server.conf:/nats-server.conf \
    docker.io/library/nats:latest -c /nats-server.conf

    Hierarchy (View Summary)

    Index
    • Creates a new instance of the driver.

      Parameters

      • options: { nc: NatsConnection; prefix?: string }

        Configuration options for the driver.

        • nc: NatsConnection

          NATS connection instance.

        • Optionalprefix?: string

          Optional prefix for NATS subjects.

      Returns NatsDriver

      If nc is not a valid NATS connection.

    • 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).