Skip to content

Commit b4bfcb1

Browse files
kaschulaGregHolmes
authored andcommitted
docs: Add notification inband mode for LiveObjects
Document the new `notification` inband mode for LiveObjects which notifies clients when objects operations are received without sending the full state data.
1 parent 2b128eb commit b4bfcb1

1 file changed

Lines changed: 146 additions & 9 deletions

File tree

src/pages/docs/liveobjects/inband-objects.mdx

Lines changed: 146 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,27 @@ If you're using LiveObjects from one of the the following languages, then use th
3737

3838
Inband objects works by delivering changes to channel objects as regular channel messages, similar to [inband occupancy](/docs/channels/options#occupancy).
3939

40+
There are two inband modes available:
41+
42+
| Mode | Description |
43+
| --- | --- |
44+
| `objects` | Sends the full state of all objects on the channel. Delivers an initial state snapshot on attach and sends updates whenever an object operation is applied. |
45+
| `notification` | Sends a notification when an object operation has been received, without the full state data. Clients can then fetch the current state via the REST API. Does not send an initial state on attach. |
46+
4047
## Enable Inband Objects <a id="inband-objects-enable"/>
4148

42-
To enable inband objects, use the `objects` [channel parameter](/docs/channels/options#objects) when getting a channel:
49+
To enable inband objects, use the `objects` [channel parameter](/docs/channels/options#objects) when getting a channel. Set the value to either `objects` or `notification` depending on which mode you want to use:
4350

4451
<Code>
4552
```javascript
46-
// When getting a channel instance
53+
// Enable full state sync mode
4754
const channelOpts = { params: { objects: 'objects' } };
4855
const channel = realtime.channels.get('my-channel', channelOpts);
4956

57+
// Or enable notification mode
58+
const channelOpts = { params: { objects: 'notification' } };
59+
const channel = realtime.channels.get('my-channel', channelOpts);
60+
5061
// Or using setOptions on an existing channel
5162
await channel.setOptions({ params: { objects: 'objects' } });
5263
```
@@ -58,10 +69,10 @@ Clients require the `channel-metadata` [capability](/docs/auth/capabilities) to
5869

5970
## Subscribe to updates <a id="inband-objects-subscribe"/>
6071

61-
When using inband objects, the client will receive special `[meta]objects` messages whenever the objects on the channel are updated. These messages provide a snapshot of the current set of objects on the channel.
72+
When using inband objects, the client will receive special `[meta]objects` messages whenever the objects on the channel are updated.
6273

6374
<Aside data-type='note'>
64-
If there is a high rate of updates to the channel objects the inband messages are throttled. However, the client is guaranteed to receive a sequence of inband messages after the last change occurs so that the latest data is always available.
75+
If there is a high rate of updates to the channel objects the inband messages are throttled. However in the case of `inband:objects`, the client is guaranteed to receive a sequence of inband messages after the last change occurs so that the latest data is always available.
6576
</Aside>
6677

6778
[Subscribe](/docs/api/realtime-sdk/channels#subscribe) to `[meta]objects` messages like you would any other message on the channel. For convenience, use a message name filter to only receive messages with the name `[meta]objects` in your listener:
@@ -70,15 +81,18 @@ If there is a high rate of updates to the channel objects the inband messages ar
7081
```javascript
7182
// Subscribe to [meta]objects messages
7283
channel.subscribe('[meta]objects', (message) => {
73-
const { syncId, nextCursor, object } = message.data;
74-
console.log("Received inband objects message:", syncId, nextCursor, JSON.stringify(message.data));
84+
console.log("Received inband objects message:", JSON.stringify(message.data));
7585
});
7686
```
7787
</Code>
7888

79-
## Message Format <a id="inband-objects-message-format"/>
89+
## Objects mode <a id="inband-objects-mode"/>
90+
91+
The `objects` mode sends the full state of all objects on the channel. When a client attaches to the channel, it immediately receives the current state. Subsequent updates are sent whenever the objects change.
8092

81-
Inband objects messages are sent as a sequence of messages, where each message contains a snapshot of a single object on the channel. Taken together, a set of messages belonging to the same sequence describes the complete set of objects on the channel.
93+
### Message format
94+
95+
Inband objects messages in `objects` mode are sent as a sequence of messages, where each message contains a snapshot of a single object on the channel. Taken together, a set of messages belonging to the same sequence describes the complete set of objects on the channel.
8296

8397
Each inband objects message has a message `name` of `[meta]objects`.
8498

@@ -90,4 +104,127 @@ The message `data` is a JSON object with the following top-level properties:
90104
| `nextCursor` | A cursor for the next message in the sequence, or `undefined` if this is the last message in the sequence. |
91105
| `object` | A JSON representation of the object included in the message. |
92106

93-
The shape of the `object` is the same as the response format of an object when listing them via the [REST API](/docs/liveobjects/rest-api-usage#fetching-objects-list-values).
107+
The shape of the `object` is the following format:
108+
109+
**Inband Counter Message**
110+
<Code>
111+
```javascript
112+
{
113+
"name": "[meta]objects",
114+
"data": {
115+
"syncId": "1148d241",
116+
"nextCursor": "root",
117+
"object": {
118+
"objectId": "counter:APjeaY7i_IQmrlxV5sEcR7NLTualutgS1QF3wNVJNYI@1769502401136",
119+
"counter": {
120+
"count": 12
121+
},
122+
"siteTimeserials": {
123+
"cbf": "01769502451073-000@cbfpt5aQQByRKW13548557:000"
124+
},
125+
"tombstone": false
126+
}
127+
}
128+
}
129+
```
130+
</Code>
131+
132+
**Inband Map Message**
133+
134+
<Code>
135+
```javascript
136+
{
137+
"name": "[meta]objects",
138+
"data": {
139+
"syncId": "95ad3a9f",
140+
"object": {
141+
"objectId": "root",
142+
"map": {
143+
"semantics": 0,
144+
"entries": {
145+
"hello": {
146+
"timeserial": "01769446125853-000@cbfpt5aQQByRKW61375059:000",
147+
"data": {
148+
"string": "world"
149+
},
150+
"tombstone": false
151+
},
152+
"votes": {
153+
"timeserial": "01769502401144-000@cbfpt5aQQByRKW13548557:001",
154+
"data": {
155+
"objectId": "counter:APjeaY7i_IQmrlxV5sEcR7NLTualutgS1QF3wNVJNYI@1769502401136"
156+
},
157+
"tombstone": false
158+
}
159+
}
160+
},
161+
"siteTimeserials": {
162+
"cbf": "01769446125853-000@cbfpt5aQQByRKW61375059:000"
163+
},
164+
"tombstone": false
165+
}
166+
}
167+
}
168+
```
169+
</Code>
170+
171+
172+
### Example
173+
174+
<Code>
175+
```javascript
176+
// Subscribe to [meta]objects messages in objects mode
177+
channel.subscribe('[meta]objects', (message) => {
178+
const { syncId, nextCursor, object } = message.data;
179+
console.log("Received object:", syncId, nextCursor, JSON.stringify(object));
180+
});
181+
```
182+
</Code>
183+
184+
## Notification mode <a id="inband-notification-mode"/>
185+
186+
The `notification` mode notifies clients when an objects operation is received, without sending the full state data. This reduces bandwidth for use cases where clients only need to know that an operation occurred and can fetch the state on demand.
187+
188+
Unlike `objects` mode, `notification` mode does not send an initial state when the client attaches to the channel. The first notification is sent immediately when the first object operation is received.
189+
190+
### Message format
191+
192+
Each notification message has a message `name` of `[meta]objects`.
193+
194+
The message `data` is a JSON object with the following property:
195+
196+
| Property | Description |
197+
| --- | --- |
198+
| `link` | A relative URL path to fetch the current state of the channel object on the channel via the [REST API](/docs/liveobjects/rest-api-usage#fetch-channel-object). This can be used directly with the Ably REST client's [request](/docs/api/rest-sdk#request) method. |
199+
200+
### Example
201+
202+
<Code>
203+
```javascript
204+
// Subscribe to [meta]objects messages in notification mode
205+
channel.subscribe('[meta]objects', async (message) => {
206+
const { link } = message.data;
207+
console.log("Objects operation received, fetching state from:", link);
208+
209+
// Fetch the current state using the REST API
210+
const response = await ablyREST.request('GET', link);
211+
if (response.items.length > 0 ) {
212+
console.log("Current state:", response.items[0]);
213+
}
214+
});
215+
```
216+
</Code>
217+
218+
### When to use notification mode
219+
220+
Use `notification` mode when:
221+
222+
* You want to minimize bandwidth usage and only fetch state when needed.
223+
* Your application can tolerate fetching state on demand rather than receiving it immediately.
224+
* You have infrequent operations and don't need continuous state updates.
225+
226+
Use `objects` mode when:
227+
228+
* You need the full state immediately on attach.
229+
* You want to receive state updates in realtime without additional REST API calls.
230+
* Your application requires continuous synchronization of state.

0 commit comments

Comments
 (0)