API v 1.2
JavaScript

History

The REST client library provides message and presence event history for channels. Channel history 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 REST 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 rest = new Ably.Rest('<loading API key, please wait>'); var channel = rest.channels.get('mud-ate-ago'); channel.publish('example', 'message data', function(err) { channel.history(function(err, resultPage) { var recentMessage = resultPage.items[0]; alert('Most recent message: ' + recentMessage.id + ' - ' + recentMessage.data); }); });
Demo Only
Copied!

If you would prefer to just dive into code and see some examples of how to use history via the REST API, then we recommend you take a look at our REST 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.

View the History API Reference.