Skip to content

Commit 82ee806

Browse files
committed
Simplify message tracking example.
1 parent f8bde26 commit 82ee806

1 file changed

Lines changed: 10 additions & 53 deletions

File tree

src/pages/docs/guides/pub-sub/notifications-center.mdx

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -405,71 +405,30 @@ Server-side tracking is also possible with [message annotations](/docs/messages/
405405

406406
#### Tracking via message ID
407407

408-
Each message has a unique [`id`](/docs/api/realtime-sdk/types#message) assigned by Ably. If your client stores the `id` of the last message it successfully processed, you can query history and stop processing when you encounter that message:
408+
Each message has a unique [`id`](/docs/api/realtime-sdk/types#message) assigned by Ably. Clients can track which messages they've processed by storing the last message ID. When reconnecting, they can query history to retrieve any missed notifications.
409409

410410
<Code>
411411
```javascript
412412
const inbox = ably.channels.get('inbox:client456');
413413

414-
// Retrieve the last processed message ID and timestamp from local storage
415-
const lastProcessedId = localStorage.getItem('lastNotificationId');
416-
const lastProcessedTimestamp = localStorage.getItem('lastNotificationTimestamp');
417-
418-
// Track whether we're still processing history
419-
let processingHistory = true;
420-
const queuedMessages = [];
421-
422-
// Subscribe to new notifications
423-
await inbox.subscribe('notification', (message) => {
424-
if (processingHistory) {
425-
// Queue messages that arrive while we're processing history
426-
queuedMessages.push(message);
427-
} else {
428-
handleNotification(message.data);
429-
localStorage.setItem('lastNotificationId', message.id);
430-
localStorage.setItem('lastNotificationTimestamp', message.timestamp.toString());
431-
}
432-
});
433-
434-
// Retrieve notifications received while offline
435-
const historyOptions = {
414+
// Query history for messages since a specific timestamp
415+
const historyPage = await inbox.history({
416+
start: lastProcessedTimestamp,
436417
limit: 100,
437-
untilAttach: true // Fetch messages from before subscription up to the attached point
438-
};
439-
440-
if (lastProcessedTimestamp) {
441-
historyOptions.start = parseInt(lastProcessedTimestamp);
442-
}
443-
444-
const historyPage = await inbox.history(historyOptions);
418+
untilAttach: true
419+
});
445420

446-
// Process historical messages (they arrive in reverse order with untilAttach)
447-
for (const message of historyPage.items.reverse()) {
448-
if (lastProcessedId && message.id === lastProcessedId) {
421+
// Process messages until you find the last one you've seen
422+
for (const message of historyPage.items) {
423+
if (message.id === lastProcessedId) {
449424
break;
450425
}
451426
handleNotification(message.data);
452-
localStorage.setItem('lastNotificationId', message.id);
453-
localStorage.setItem('lastNotificationTimestamp', message.timestamp.toString());
454-
}
455-
456-
// Now process any messages that arrived while we were handling history
457-
processingHistory = false;
458-
for (const message of queuedMessages) {
459-
handleNotification(message.data);
460-
localStorage.setItem('lastNotificationId', message.id);
461-
localStorage.setItem('lastNotificationTimestamp', message.timestamp.toString());
462427
}
463428
```
464429
</Code>
465430

466-
The `start` parameter is inclusive and helps limit the query range to messages from that timestamp onwards, but you still need to check `message.id` to detect which specific message you last processed. This approach works because history with `untilAttach: true` returns messages in reverse order, allowing you to paginate backwards and stop when you find the last message you've already seen.
467-
468-
Message history is particularly useful for notifications that clients need to see when they return, but that don't require immediate push notification delivery. This is common in internal systems where notifications might inform clients of completed processes, status updates, or system alerts that can be reviewed when a client connects.
469-
470-
<Aside data-type='note'>
471-
History returns a paginated list of messages. As such, you may need to paginate through multiple pages to find the last processed message, depending on how many messages were sent while the client was offline.
472-
</Aside>
431+
History queries can be paginated if needed, allowing you to retrieve larger sets of missed notifications across multiple requests.
473432

474433
#### Tracking via message annotations
475434

@@ -572,8 +531,6 @@ Before launching your notification center, review these key points:
572531
573532
* **Authentication:** Use JWT authentication for all client-side communication with short TTLs (1-4 hours max).
574533
* **Capabilities:** Apply the principle of least privilege - clients should only have subscribe access to their own inbox channels.
575-
* **Validation:** Validate all notification request data in your backend, never trust client data.
576-
* **Monitoring:** Subscribe to [`[meta]log` channels](/docs/platform/errors#meta) to track publishing errors and issues.
577534
* **Rate limiting:** Implement rate limiting in your backend to help prevent abuse.
578535
* **Scalability:** Ensure your backend can scale horizontally to handle request and publishing load.
579536
* **Cost monitoring:** Set up billing alerts and monitor usage patterns to optimize costs.

0 commit comments

Comments
 (0)