History

The history feature allows you to view a list of messages previously broadcast to a room. You can apply filters to retrieve messages from a specific time range or to extract a particular subset of messages.

By default, messages broadcast to a room are stored for 24 hours. However, this storage period can be extended up to a maximum of 30 days through the dashboard settings.

Guaranteed Delivery

Message history is accessible at the room level. When you join a room, you have the ability to retrieve the message history at any time. By leveraging this feature, you can ensure a guaranteed delivery mechanism for your application.

let disconnectionTime = 0;

const room = await relayBox.join('myRoom');

relaybox.connection.on('disconnect', () => {
  disconnectionTime = Date.now();
});

relayBox.connection.on('reconnected', () => {
  room.history.get({ start: disconnectionTime })
});

The code above implements a basic reconnection strategy. When a connection is lost, the client will automatically attempt to reconnect. If the reconnection is successful, the client will retrieve the message history for the period during which the connection was lost. This ensures that the client receives any messages broadcast while it was offline.

Lazy Loading

Message history can be leveraged to support lazy loading. The next() method, which is provided with each batch of messages, allows you to retrieve a subset of messages along with the next iterator. You can call the iterator functions at any time to load the next batch of messages.

const myRoom = relayBox.join('myRoom');

const history = await myRoom.history.get();

if (history.next) {
  const nextHistory = await history.next();
}

Batching

To avoid overloading the server, the history service will return batches of messages. The maximum batch size is anything up to 100 messages. If the number of messages exceeds 100, the history response will return an iterator function that can be used to retrieve the next batch of messages until all messages have been retrieved.

The following example demonstrates how to retrieve all messages in a room using a batching approach.

const history = await myRoom.history.get();

while (history.next) {
  history = await history.next();
  console.log(history.items);
}

    On this page