Options
All
  • Public
  • Public/Protected
  • All
Menu

Class WebSocketNetwork

Syncs updates to Collabs documents via a server over WebSockets.

The server is in package @collabs/ws-server.

This class is designed to work seamlessly with other sources of updates, such as @collabs/indexeddb. In particular, updates from those sources will be synced to the server alongside local operations.

  • Exception: Updates from other tabs via @collabs/tab-sync are not sent, since the source tab should send them.

Hierarchy

Index

Constructors

  • new WebSocketNetwork(url: string, options?: { connect?: boolean }): WebSocketNetwork
  • Constructs a WebSocketNetwork.

    You typically only need one WebSocketNetwork per app, since it can subscribe multiple documents.

    Parameters

    • url: string

      The WebSocket url to connect to.

    • options: { connect?: boolean } = {}
      • Optional connect?: boolean

        Set to false to skip connecting in the constructor. If so, you will need to call connect later.

    Returns WebSocketNetwork

Accessors

  • get connected(): boolean

Methods

  • close(): void
  • connect(): void
  • Connects to the server.

    Once connected, all subscribed docs sync their current states with the server, then send and receive further updates as they occur.

    If we disconnect due to a network error, you will need to call this method again to reconnect. E.g., to try to reconnect every 2 seconds after a disconnection:

    wsNetwork.on("Disconnect", () => setTimeout(wsNetwork.connect(), 2000));
    

    (Since failed connection attempts also emit a "Disconnect" event, this will repeat until a connection succeeds.)

    Returns void

  • disconnect(): void
  • Emits an event, which triggers all the registered event handlers.

    Event handlers are called in the order they are added. Errors in event handlers are captured and logged (with console.error), not propagated to the caller.

    Type Parameters

    Parameters

    Returns void

  • Registers an event handler that is triggered when the event happens.

    Type Parameters

    Parameters

    Returns (() => void)

    An "off" function that removes the event handler when called.

      • (): void
      • Registers an event handler that is triggered when the event happens.

        Returns void

        An "off" function that removes the event handler when called.

  • setBatchSendMS(doc: AbstractDoc | CRuntime, batchSendMS: null | number): void
  • subscribe(doc: AbstractDoc | CRuntime, docID: string, options?: { batchRemoteMS?: number; batchSendMS?: number }): void
  • Subscribes doc to updates for docID.

    doc will send and receive updates with the server's copy of docID. It will also sync initial states with the server, to ensure that doc and the server start up-to-date.

    throws

    If doc is already subscribed to a docID.

    throws

    If another doc is subscribed to docID.

    Parameters

    • doc: AbstractDoc | CRuntime

      The document to subscribe.

    • docID: string

      An arbitrary string that identifies which updates to use.

    • options: { batchRemoteMS?: number; batchSendMS?: number } = {}
      • Optional batchRemoteMS?: number

        If set, remote updates to doc are delivered at most once every batchRemoteMS ms, emitting only a single doc "Change" event. This limits render frequency.

      • Optional batchSendMS?: number

        Experimental If set, local updates to doc are pushed to the server at most once every batchSendMS ms. This reduces the load on the server and on remote users, but increases the time before a local update is saved and visible remotely.

    Returns void

  • unsubscribe(doc: AbstractDoc | CRuntime): void
  • Unsubscribes doc from its subscribed docID (if any).

    doc will no longer send or receive updates with the server.

    Parameters

    • doc: AbstractDoc | CRuntime

    Returns void

Generated using TypeDoc