Lifecycle events

Open in

Handle synchronization events

The channel.object instance emits synchronization events that indicate when the local state on the client is being synchronized with the Ably service. These events can be useful for displaying loading indicators, preventing user interactions during synchronization, or triggering application logic when data is first loaded.

EventDescription
syncingEmitted when the local copy of objects on a channel begins synchronizing with the Ably service.
syncedEmitted when the local copy of objects on a channel has been synchronized with the Ably service.

1

2

3

4

5

6

7

8

9

channel.object.on('syncing', () => {
  console.log('Objects are syncing...');
  // Show a loading indicator and disable edits in the application
});

channel.object.on('synced', () => {
  console.log('Objects have been synced.');
  // Hide loading indicator
});

Handle object lifecycle events

Detect object deletion

Objects can become unreachable when they are removed from the object tree using remove() and never reassigned. Unreachable objects are garbage collected by Ably, typically after 24 hours.

Since deleted objects are unreachable via any path, object.delete events can only be received through Instance subscriptions, which track a specific object by its ID regardless of its location in the object tree:

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

const counterInstance = myObject.get('visits').instance();

if (counterInstance) {
  counterInstance.subscribe(({ object, message }) => {
    if (message?.operation.action === 'object.delete') {
      console.log('Counter instance was deleted');
      // The client automatically unsubscribes the subscription after this notification
      // Remove references to this object from your application
    } else {
      console.log('Counter updated:', object.value());
    }
  });
}

In most cases, you don't need to explicitly handle object deletion. Your application should have already reacted to object removal when a corresponding LiveMap.remove() operation was received. However, if your application separately stores references to object instances and does not properly clear them when objects are orphaned, any later interactions with those objects after they are deleted will result in an error. In such cases, handling deletion events via Instance subscriptions helps ensure that those references are cleaned up and runtime errors are avoided.