Operations

LiveObjects operations define how object data is updated and synchronized across multiple clients.

When you create or update an object, the change is expressed as an operation that is sent as an object message on the channel. The operation is then applied to the object instance on all clients that are subscribed to the channel.

This document explains the key concepts you need to know when working with operations.

Operation Types

Each object type supports specific operations that modify the object's data.

LiveMap Operations

LiveMap supports the following operations:

  • set: Set a value for a key
  • remove: Remove a key and its value

The value of an entry in a LiveMap instance can be a primitive type or a reference to another object.

JavaScript

LiveCounter Operations

LiveCounter supports the following operations:

  • increment: Increment the counter by a specified amount
  • decrement: Decrement the counter by a specified amount

The amount is a double-precision floating-point number, which is the same as underlying type of a LiveCounter value.

JavaScript

Create Operations

Create operations are used to instantiate new objects of a given type.

A create operation can optionally specify an initial value for the object.

JavaScript

When a create operation is processed, an object ID for the new object instance is automatically generated for the object.

Object IDs

Every operation is expressed relative to a specific object instance, identified by its object ID, which determines which object the operation is applied to.

When using a client library object IDs are handled automatically, allowing you work directly with object references:

JavaScript

Therefore it is important that you obtain an up-to-date object instance before performing operations on an object. For example, you can subscribe to a LiveMap instance to ensure you always have an up-to-date reference to any child objects in the map:

JavaScript

In the REST API, this relationship is made explicit:

curl -X POST https://rest.ably.io/channels/my-channel/objects \
 -u "{{API_KEY}}"
 -H "Content-Type: application/json" \
 --data \
'{
  "operation": "MAP_SET",
  "objectId": "root",
  "data": {"key": "username", "value": {"string": "alice"}}
}'

Batch Operations

Batch operations can be used to batch a set of operations together:

  • Multiple operations are grouped into a single atomic unit
  • All operations in the batch either succeed together or fail together
  • Operations in a batch are sent as a single message
  • No operations from other clients can be interleaved within a batch
Select...