Type of state object being synchronized. This object should be a simple JSON object that uses only serializable primitives.
Creates a new LiveObjectSynchronizer
instance.
ID of the live object being synchronized. This should be the value of this.id
in a class that derives from DataObject
.
^ @param runtime The objects local runtime. This should be the value of this.runtime
.
The runtime for the objects container. This should be the value of this.context.containerRuntime
.
A function called to retrieve the objects current state. This will be called prior to a "connect" or "update" message being sent.
A function called to process a state update received from a remote instance. This will be called anytime a "connect" or "update" message is received.
Disposes of the synchronizer.
Generated using TypeDoc
Synchronizes the underlying state of an live object with all of the other instances of the object connected to the same container.
When a synchronizer for a live object is first created it will broadcast a
"connect"
message, containing the objects initial state, to all other instances of the object that are currently running on other clients. Those instances will respond to the sent "connect" message by broadcasting an"update"
message containing the current state of their object.Anytime a remote "connect" or "update" event is received, the synchronizer will call the passed in
updateState
callback with the remote objects state and the senders clientId for role verification purposes. The logic for processing these state updates will vary but implementations will generally want to include a timestamp in their state update so that clients can protect against out-of-order and delayed updates. Deriving your state update fromILiveEvent
and usingLiveEvent.isNewer
to compare the received update with the current update makes this simple.Once the initial "connect" event is sent, the synchronizer will periodically broadcast additional "update" events containing the live objects current state. This redundancy helps to guard against missed events and can be used as a ping for scenarios like presence where users can disconnect from the container without notice. The rate at which these ping events are sent can be adjusted globally by setting the static
LiveObjectSynchronizer.updateInterval
property.While each new synchronizer instance will result in a separate "connect" message being sent, the periodic updates that are sent get batched together into a single "update" message. This lets apps add as many live objects to a container as they'd like without increasing the number of messages being broadcast to the container.
Only a single synchronizer is allowed per live object. Attempting to create more than one synchronizer for the same live object will result in an exception being raised.