You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Includes hydration with rewind and hydration with persisted history +
untilAttach. Describes the pattern for handling in-progress live
responses with complete responses loaded from the database.
When clients connect or reconnect, such as after a page refresh, they often need to catch up on tokens that were published while they were offline or before they joined. Ably provides several approaches to hydrate client state depending on your application's requirements.
237
+
238
+
<Aside data-type="note">
239
+
If you need to retrieve and process large amounts of historical data, consider using the [message-per-response](/docs/ai-transport/features/token-streaming/message-per-response) pattern instead, in which the complete response appears as a single message in history.
240
+
</Aside>
241
+
242
+
### Using rewind for recent history <a id="rewind"/>
243
+
244
+
The simplest approach is to use Ably's [rewind](/docs/channels/options/rewind) channel option to automatically retrieve recent tokens when attaching to a channel:
245
+
246
+
<Code>
247
+
```javascript
248
+
// Use rewind to receive recent historical messages
params: { rewind:'2m' } // or rewind: 100 for message count
251
+
});
252
+
253
+
// Subscribe to receive both recent historical and live messages,
254
+
// which are delivered in order to the subscription
255
+
awaitchannel.subscribe('token', (message) => {
256
+
consttoken=message.data;
257
+
258
+
// Process tokens from both recent history and live stream
259
+
console.log('Token received:', token);
260
+
});
261
+
```
262
+
</Code>
263
+
264
+
Rewind supports two formats:
265
+
266
+
- **Time-based**: Use a time interval like `'30s'` or `'2m'` to retrieve messages from that time period
267
+
- **Count-based**: Use a number like `50` or `100` to retrieve the most recent N messages (maximum 100)
268
+
269
+
<Aside data-type="note">
270
+
At most 100 messages will be retrieved in a rewind request. If more messages exist within the specified interval, only the most recent 100 are sent.
271
+
</Aside>
272
+
273
+
By default, rewind is limited to the last 2 minutes of messages. This is usually sufficient for scenarios where clients need only recent context, such as for continuous token streaming, or when the response stream from a given model request does not exceed 2 minutes. If you need more than 2 minutes of history, see [Using history for longer persistence](#history).
274
+
275
+
### Using history for longer persistence <a id="history"/>
276
+
277
+
For applications that need to retrieve tokens beyond the 2-minute rewind window, enable [persistence](/docs/storage-history/storage#all-message-persistence) on your channel. Use [channel history](/docs/storage-history/history) with the [`untilAttach` option](/docs/storage-history/history#continuous-history) to paginate back through history to obtain historical tokens, while preserving continuity with the delivery of live tokens:
278
+
279
+
<Code>
280
+
```javascript
281
+
// Use a channel in a namespace called 'persisted', which has persistence enabled
// Subscribe to live messages (implicitly attaches the channel)
287
+
awaitchannel.subscribe('token', (message) => {
288
+
// Append the token to the end of the response
289
+
response +=message.data;
290
+
});
291
+
292
+
// Fetch history up until the point of attachment
293
+
let page =awaitchannel.history({ untilAttach:true });
294
+
295
+
// Paginate backwards through history
296
+
while (page) {
297
+
// Messages are newest-first, so prepend them to response
298
+
for (constmessageofpage.items) {
299
+
response =message.data+ response;
300
+
}
301
+
302
+
// Move to next page if available
303
+
page =page.hasNext() ?awaitpage.next() :null;
304
+
}
305
+
```
306
+
</Code>
307
+
308
+
### Hydrating an in-progress live response <a id="live-response"/>
309
+
310
+
A common pattern is to persist complete model responses in your database while using Ably for live token delivery of the in-progress response.
311
+
312
+
The client loads completed responses from your database, then reaches back into Ably channel history until it encounters a token for a response it's already loaded.
313
+
314
+
You can retrieve partial history using either the [rewind](#rewind) or [history](#history) pattern.
315
+
316
+
#### Hydrate using rewind
317
+
318
+
Load completed responses from your database, then use rewind to catch up on any in-progress responses, skipping any tokens that belong to a response that was already loaded:
Load completed responses from your database, then paginate backwards through history to catch up on in-progress responses until you reach a token that belongs to a response you've already loaded:
0 commit comments