diff --git a/docs/migration-guides/v2/liveobjects.md b/docs/migration-guides/v2/liveobjects.md new file mode 100644 index 0000000000..123aec8d98 --- /dev/null +++ b/docs/migration-guides/v2/liveobjects.md @@ -0,0 +1,874 @@ +# LiveObjects migration guide for ably-js v2.16 + +## Overview + +ably-js v2.16 introduces significant improvements to the LiveObjects API, centered around a new path-based interaction model using `PathObject`. While these are breaking changes, they provide a more intuitive and robust experience. + +**Key improvements:** + +- **Path-based operations**: Interact with nested objects through paths that automatically resolve at runtime +- **Resilient subscriptions**: Subscribe to paths rather than specific object instances, making subscriptions resilient to object replacements +- **Simplified object creation**: Create deeply nested object structures in a single operation without explicit management of child objects or risk of creating orphaned objects + +Here's how to migrate your LiveObjects usage to the new PathObject-based API introduced in ably-js v2.16: + +1. [Understand `PathObject`](#understand-pathobject). +2. [Update to v2.16 or later and handle breaking changes](#update-to-v216-or-later-and-handle-breaking-changes). +3. (Optional) [Take advantage of new LiveObjects features that v2.16 introduces](#take-advantage-of-new-liveobjects-features-that-v216-introduces). +4. (Optional) Check out [common migration patterns](#common-migration-patterns) for a quick reference. + +## Understand `PathObject` + +The core concept in the new API is the `PathObject`. Unlike the previous API where you worked directly with `LiveMap` and `LiveCounter` instances, a `PathObject` represents a **path to a location** within your channel's object hierarchy. + +**Why path-based?** The previous instance-based approach had several limitations: + +- Traversing object hierarchy required explicit checks for nulls to check if an object exists +- Instance-level subscriptions broke when an object at a path was replaced with a new instance +- Instance-level subscriptions for collection types lacked the ability to subscribe and receive updates for nested child objects + +With `PathObject`, operations are evaluated against the current value at a path **when the operation is invoked**, not when the `PathObject` is created. This makes your code more resilient to changes in the object structure. + +You can still access the specific underlying `Instance` using [`PathObject.instance()`](#access-explicit-object-instances-using-instance) when needed. + +## Update to v2.16 or later and handle breaking changes + +Begin by updating to ably-js version 2.16.0 or later. + +Now, you need to address the breaking changes introduced by v2.16. Here we explain how. + +The changes below are split into: + +- [general changes](#general-changes) +- [changes that only affect TypeScript users](#only-typescript-users) + +### General changes + +#### Update LiveObjects plugin import + +The LiveObjects plugin import has changed in several ways: + +1. The import path has changed from `'ably/objects'` to `'ably/liveobjects'` +2. The plugin is now a named export instead of a default export +3. The plugin name has changed from `Objects` to `LiveObjects`, which also affects the key used in the `plugins` client option + +**Before:** + +```typescript +import * as Ably from 'ably'; +import Objects from 'ably/objects'; + +const client = new Ably.Realtime({ + key: 'your-api-key', + plugins: { Objects }, +}); +``` + +**After:** + +```typescript +import * as Ably from 'ably'; +import { LiveObjects } from 'ably/liveobjects'; + +const client = new Ably.Realtime({ + key: 'your-api-key', + plugins: { LiveObjects }, +}); +``` + +**Note:** If you're using the UMD bundle via a `