Skip to main content
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 an Instance from a PathObject using the instance() method:
The 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:
An Instance references a specific object by its ID rather than by location.

Type inference

When using TypeScript, you can provide type parameters to instance() to get rich type inference:
For LiveMap instances, use the get(key) method to navigate to a child value:
The id getter returns the object ID of the instance, which can be used with the REST API.

Access data

Use the get(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 or LiveCounter stored directly in the LiveMap, use the value() method to get the current value:
If the entry doesn’t exist, get() returns undefined:
If the entry exists, but does not contain a primitive or a LiveCounter instance, value() returns undefined:

Enumerate collections

For LiveMap instances, you can iterate over the entries, keys, and values:

Get the size of a collection

Use the size() method to get the number of entries in a LiveMap:

Get a compact object

The compact() method returns a JavaScript object representation of the instance:
Binary data in the channel object is preserved as typed arrays in the returned object. It is possible for the value returned from 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():
Binary data in the channel object are serialized as base64-encoded strings by 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 LiveMap instances, you can use methods like set() and remove(). See the LiveMap documentation for details on these methods.
  • For LiveCounter instances, you can use methods like increment() and decrement(). See the LiveCounter documentation for details on these methods.
When you call a method on an Instance, it directly updates that specific instance.

Batch multiple updates

The batch(callback) method groups multiple mutations into a single message. The ctx parameter is the batch context for the specific instance:
See the Batch operations documentation for more details on batching.

Subscribe to changes

Use the subscribe() method to be notified when the instance is updated:
Alternatively, use the 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 object field contains an Instance representing the same object instance you called subscribe() on
  • The message field contains the ObjectMessage which details the operation that caused the change, including information about the client that performed the operation and the specific changes made.

Updates to nested objects

Instance subscriptions only observe changes to the specific instance that is subscribed. If an update is made to a nested child object, that is not surfaced on an instance subscription: