Create a batch context
To batch object operations together, use thebatch() method on a PathObject or Instance. This method accepts a callback function that receives a batch context that allows you to construct operations. Ably publishes these operations together as a single channel message. If an error occurs publishing the batched operations, all operations are discarded, preventing partial updates and ensuring atomicity.
Call batch() on a PathObject to group operations on that path:
batch() on an PathObject whose path resolves to a LiveMap or LiveCounter instance:
batch() on an Instance to batch operations on that specific object:
Create objects in a batch
You can create new objects inside a batch usingLiveMap.create() and LiveCounter.create(). This allows you to atomically create and assign multiple objects in a single operation:
Navigate a batch context
Navigate to nested objects using theget() method on the batch context. Navigating a batch context with get() has the same behaviour as navigating an Instance:
Cancel a batch
To explicitly cancel a batch before it is applied, throw an error inside the batch function. This prevents any queued operations from being applied:Understand batch context behavior
The batch context has the same API as Instance, except forbatch() itself, but all mutation methods are synchronous and queue operations instead of sending them immediately. After the callback completes, all queued operations are sent together in a single channel message.
