API v 1.2
JavaScript

History

The Realtime client library provides message and presence event history for channels. Channel history can be used to return continuous message history up to the exact point a realtime channel was attached, and combines both instantaneous “live” history as well as the longer term persisted history. If persisted history is enabled for the channel, then messages will typically be stored for 24 – 72 hours on disk. If persisted history is not enabled, Ably retains the last two minutes of instantaneous “live” message history in memory.

The Ably Realtime client library provides a straightforward API to retrieve paginated message or presence event history. Each page of history, by default, contains up to 100 messages. Message ordering, by default, is from most recent to oldest.

JavaScript
var realtime = new Ably.Realtime('<loading API key, please wait>'); var channel = realtime.channels.get('mud-ate-ago'); channel.publish('example', 'message data', function(err) { channel.history(function(err, resultPage) { var lastMessage = resultPage.items[0]; alert('Last message: ' + lastMessage.id + ' - ' + lastMessage.data); }); });
Demo Only
Copied!

Note that all examples on this page assume you are running them within an EventMachine reactor. Find out more in our Realtime usage documentation

If you would prefer to just dive into code and see some examples of how to use history via the Realtime API, then we recommend you take a look at our Realtime tutorials.

Both the Channel and Presence objects provide history. The Channel object provides the history of Message objects published on the channel, whereas the Presence object provides presence event history of that channel i.e. members entering, updating or leaving the channel as PresenceMessage objects.

By default, persisted history on channels is disabled and messages are only stored by the Ably service for two minutes in memory. If persisted history is enabled for the channel, then messages will typically be stored for 24 – 72 hours on disk.

Every message that is persisted to or retrieved from disk counts as an extra message towards your monthly quote. For example, for a channel that has persistence enabled, if a message is published, two messages will be deducted from your monthly quota. If the message is later retrieved from history, another message will be deducted from your monthly quota.

To enable history on a channel, it is necessary to add a channel rule in the settings of your application dashboard. See the documentation on channel rules for further information on what they are and how to configure them.

By using rewind or History’s untilAttach, it is possible to obtain message history that is continuous with the realtime messages received on an attached channel.

If you wish to obtain history as part of attaching to a channel, you can use the rewind channel parameter. This will act as though you had attached to a channel from a certain message or time in the past, and play through all messages since that point. Rewind can only be used when first attaching to a channel.

A rewind value that is a number (n) is a request to attach to the channel at a position n messages before the present position. rewind can also be a time interval, specifying a number of seconds (15s) or minutes (1m) to replay messages from.

The following example will subscribe to the channel and relay the last 3 messages:

JavaScript
const realtime = new Ably.Realtime('<loading API key, please wait>'); realtime.channels.get('mud-ate-ago', { params: {rewind: '3'} }).subscribe(msg => console.log("Received message: ", msg));
Demo Only
Copied!

Note: You can also qualify a channel name with rewind when using the service without a library, such as with SSE or MQTT.

It is possible to obtain message history that is continuous with the realtime messages received on an attached channel, in the backwards direction from the point of attachment. When a Channel instance is attached, it’s automatically populated by the Ably service with the serial number of the last published message on the channel. As such, using this serial number, the client library is able to make a history request to the Ably service for all messages received since the channel was attached. Any new messages therefore are received in realtime via the attached channel, and any historical messages are accessible via the history method.

In order to benefit from this functionality, the untilAttach option can be used when making history requests on attached channels. If the channel is not yet attached, this will result in an error.

JavaScript
var realtime = new Ably.Realtime('<loading API key, please wait>'); var channel = realtime.channels.get('mud-ate-ago'); channel.attach(function(err) { channel.history({ untilAttach: true}, function(err, resultPage) { var lastMessage = resultPage.items[0]; alert('Last message before attach: ' + lastMessage.data); }); });
Demo Only
Copied!

View the History API Reference.