Lifecycle events

The channel.objects 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.

  • syncing – Emitted when the local copy of objects on a channel begins synchronizing with the Ably service.
  • synced – Emitted when the local copy of objects on a channel has been synchronized with the Ably service.
JavaScript v2.9
channel.objects.on('syncing', () => { console.log('Objects are syncing...'); // Show a loading indicator and disable edits in the application }); channel.objects.on('synced', () => { console.log('Objects have been synced.'); // Hide loading indicator });
Copied!

Lifecycle events enable you to monitor changes in an object’s lifecycle.

Currently, only the deleted event can be emitted. Understanding the conditions under which this event is emitted and handling it properly ensures that your application maintains expected behavior.

Objects that were created on a channel can become orphaned when they were never assigned to the object tree, or because their reference was removed using LiveMap.remove() and never reassigned. Orphaned objects will be garbage collected by Ably, typically after 24 hours. When this happens, a deleted event is broadcast for the affected object. Once deleted, an object can no longer be interacted with, and any operations performed on it will result in an error.

While the LiveObjects feature internally manages object deletions and removes them from its internal state, your application may still hold references to these deleted objects in separate data structures. The deleted event provides a way to react accordingly by removing references to deleted objects and preventing potential errors.

In most cases, subscribing to deleted events is unnecessary. 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, subscribing to deleted events helps ensure that those references are cleaned up and runtime errors are avoided.

JavaScript v2.9
const { off } = counter.on('deleted', () => { console.log('LiveCounter has been deleted.'); // Remove references to this object from your application // as it can no longer be interacted with });
Copied!

Read more about subscribing to object lifecycle events for LiveCounter and LiveMap.

Select...