API Reference
    Preparing search index...

    Class NatsDriver

    NATS-based signaling driver for inter-process communication.

    This driver uses NATS as the underlying messaging system, allowing for distributed signaling across multiple browsers and devices. It supports optional encryption of messages using AES-GCM with a secret key and namespace hashing using SHA-256 for obfuscation.

    This driver requires the nats.ws library for WebSocket-based NATS connections directly in the browser.

    import { connect } from 'https://esm.sh/nats.ws';

    const driver = new NatsDriver({
    connect: async () => await connect({ servers: ['wss://demo.nats.io:8443'] }),
    secret: 'your-secret-key',
    });

    await driver.open();

    Implements

    Index

    Constructors

    Methods

    Constructors

    • Create a new instance of the driver.

      Parameters

      • options: { connect: (config?: any) => Promise<any>; prefix?: string; secret?: string }

        Configuration options for the driver.

        • connect: (config?: any) => Promise<any>

          A function that returns a promise resolving to a NATS connection instance.

        • Optionalprefix?: string

          An optional prefix for NATS subjects.

        • Optionalsecret?: string

          An optional secret key for encrypting messages.

      Returns NatsDriver

    Methods

    • Closes the connection to the NATS server.

      Returns Promise<void>

    • Publish a signaling message to a namespace.

      Parameters

      • namespace: string[]

        Target namespace segments.

      • message: any

      Returns Promise<void>

    • Unsubscribe a previously registered namespace handler.

      Parameters

      • namespace: string[]

        Namespace segments used for message routing.

      • handler: (data: any) => void

        Handler reference originally passed to on.

      Returns Promise<void>

    • Subscribe to signaling messages in a namespace.

      Parameters

      • namespace: string[]

        Namespace segments used for message routing.

      • handler: (data: any) => void

        Callback invoked with message payload.

      Returns Promise<void>

    • Opens the connection to the NATS server and initializes encryption if a secret is provided.

      Parameters

      • Optionalconfig: any

        Optional configuration options.

      Returns Promise<void>