History

History allows you to retrieve messages that were previously published to a room. Filters can be applied to retrieve messages from a specific time range or to extract a subset of messages.

To view the full documentation, please refer to the API reference.

Guaranteed Delivery

Message history is accessible at the room level. Once a client has joined a room, it has the ability to retrieve the message history for that room at any time. By leveraging this feature, you can create 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 message history for the period during which the connection was unavailable is retrieved. This ensures that the client receives any messages broadcast while it was offline.

Lazy Loading

History can also assist with creating a lazy loading experience. 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();

// Then, later...

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

History maintains the current message cursor position, meaning that any time next() is called, the next batch of messages will be retrieved in the same order, starting from the cursor position.

    On this page