Instance provides a reference to a specific LiveObject instance (such as a LiveMap or LiveCounter) that you can manipulate directly. Unlike PathObject, which operates on paths that resolve dynamically at runtime, an Instance is bound to a specific object instance identified by its object ID.
An Instance represents a specific object instance within the channel object. When you call methods on an Instance, they operate on that specific instance regardless of where it exists in the structure or whether it has been moved.
Get an Instance
Obtain anInstance from a PathObject using the instance() method:
instance() method returns undefined if no object exists at that path.
Once you have an Instance, it references a specific object by its ID. This means operations target that exact object:
Instance references a specific object by its ID rather than by location.
Type inference
When using TypeScript, you can provide type parameters toinstance() to get rich type inference:
Navigate an Instance
ForLiveMap instances, use the get(key) method to navigate to a child value:
id getter returns the object ID of the instance, which can be used with the REST API.
Access data
Use theget(key) method to access a value from a LiveMap instance. The returned Instance represents whatever exists at that entry. What you can do with that Instance depends on whether the entry contains a primitive value, a LiveCounter, or a nested LiveMap.
Read values
When an entry contains a primitive value orLiveCounter stored directly in the LiveMap, use the value() method to get the current value:
get() returns undefined:
LiveCounter instance, value() returns undefined:
Enumerate collections
ForLiveMap instances, you can iterate over the entries, keys, and values:
Get the size of a collection
Use thesize() method to get the number of entries in a LiveMap:
Get a compact object
Thecompact() method returns a JavaScript object representation of the instance:
compact() to contain cyclic references, so it is not safe to serialize this value with JSON.stringify().
Use the compactJson() method to obtain a value that can be safely passed to JSON.stringify():
compactJson().
Update data
Instance provides mutation methods that operate on the specific instance. The specific methods available depend on the type of the instance:
- For
LiveMapinstances, you can use methods likeset()andremove(). See the LiveMap documentation for details on these methods. - For
LiveCounterinstances, you can use methods likeincrement()anddecrement(). See the LiveCounter documentation for details on these methods.
Instance, it directly updates that specific instance.
Batch multiple updates
Thebatch(callback) method groups multiple mutations into a single message. The ctx parameter is the batch context for the specific instance:
Subscribe to changes
Use thesubscribe() method to be notified when the instance is updated:
subscribeIterator() method for an async iterator syntax:
Instance subscriptions observe a specific object instance rather than a location. If the instance is moved to a different location within the channel object, the subscription continues to observe the same instance.
When an object instance is deleted after becoming unreachable, subscriptions to that instance are automatically unsubscribed after one final notification:
Determine what changed
The subscription receives an argument with information about the update:- The
objectfield contains anInstancerepresenting the same object instance you calledsubscribe()on - The
messagefield contains theObjectMessagewhich details the operation that caused the change, including information about the client that performed the operation and the specific changes made.
