Constructs a WebSocketNetwork.
You typically only need one WebSocketNetwork per app, since it can subscribe multiple documents.
The WebSocket url to connect to.
Set to false to skip connecting in the constructor. If so, you will need to call connect later.
Whether we have an open WebSocket connection to the server.
Closes our WebSocket and unsubscribes all documents.
Future method calls will throw an error.
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.)
Disconnects from the server or aborts an in-progress connection attempt.
You may reconnect later by calling connect.
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.
Name of the event to emit.
Event object to pass to the event handlers.
Registers an event handler that is triggered when the event happens.
Name of the event to listen on.
Callback that handles the event.
If true, the event handler is triggered at most once (the next time the event happens), then unsubscribed.
An "off" function that removes the event handler when called.
Registers an event handler that is triggered when the event happens.
An "off" function that removes the event handler when called.
Experimental
Changes the batchSendMS
for a subscribed doc.
See subscribe's options.batchSendMS
.
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.
The document to subscribe.
An arbitrary string that identifies which updates to use.
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.
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.
Unsubscribes doc
from its subscribed docID
(if any).
doc
will no longer send or receive updates with the server.
Generated using TypeDoc
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.