API v 1.2


Ably Platform allows for clients to send information with messages, which contain data the client wishes to communicate. These messages are published through channels, which other users can subscribe to in order to receive them. This scalable and resilient messaging pattern is commonly called pub/sub.

The Ably REST client library provides a straightforward API for publishing messages and retrieving message history from a channel.

var rest = new Ably.Rest('<loading API key, please wait>'); var channel = rest.channels.get('aft-hip-jab'); channel.publish('example', 'message data', function() { channel.history(function(err, resultPage) { console.log('Last published message:' + resultPage.items[0]); }); });
Demo Only

If you would prefer to just dive into code and see some examples of how to use messages, then we recommend you take a look at our REST tutorials.

Each message published has an optional event name property and a data property carrying the payload of the message. Various primitive and object types are defined, portable and supported in all clients, enabling clients to be interoperable despite being hosted in different languages or environments.

The supported payload types are Strings, objects or arrays capable of JSON representation, buffers containing arbitrary binary data, and Nulls. Client libraries will detect the supplied message payload and encode the message appropriately. (Note that if sending a binary, that binary should be the entire payload; an object with a binary field within it may not be correctly encoded).

The REST client library does not offer message realtime subscription but instead provides access to the “live” history using the REST history API. Find out more about subscribing to messages in realtime using the Realtime API.

The name property of published messages does not affect the distribution of a channel message to clients but may be used as a subscription filter, allowing a client to register a listener that only sees a subset of the messages received on the channel. Find out more about registering listeners using the Realtime API.

Channels expose a publish method whereby a client can publish either a single message or an array of messages to a channel over REST.

channel.publish('event', 'This is my payload', function(err) { if(err) { console.log('Unable to publish message; err = ' + err.message); } else { console.log('Message successfully sent'); } });

It is common for a single message to be intended for multiple channels. If you wish to send a message to multiple channels within a single operation, you can make use of the REST batch API. With a realtime connection, you can also effectively send a message to multiple channels at once by allowing multiple concurrent publish operations.

Idempotency ensures that multiple publishes of the same message cannot result in duplicate messages. Find out more about what idempotency is, and how we provide idempotency in our REST operations.

It is possible for a client publishing through REST to not receive an acknowledgement of receipt from Ably for numerous reasons such as network failures outside of our control. This can lead to the client automatically re-publishing the message, which is a feature of our SDKs to help route around network failures. We recommend that you enable idempotent publishing in our REST SDKs if you want to avoid these retry attempts potentially resulting in duplicate messages. When enabled, the REST SDK automatically assigns a unique ID to each message ensuring subsequent retries cannot result in duplicates.

If you provide a unique ID with each message published, all publishes will be idempotent – that is you can freely publish the message from one or more workers without worrying about duplicates. Please note that publishing multiple messages in a single publish operation with client-specified ids has many constraints you need to be aware of.

Below is an example of how to provide a unique ID:

var rest = new Ably.Rest('<loading API key, please wait>'); var channel = rest.channels.get('aft-hip-jab'); channel.publish([{data: 'payload', id: 'unique123'}]);
Demo Only

Message published using the REST API may be done so on behalf of an existing realtime connection when a valid connectionKey is present in the published message. For example, if you want to publish a message using the REST client library so that it appears to come from an existing connected realtime client, then the connection’s private (secret) connection key must be included.

If the connectionKey is invalid or belongs to a connection that has since been closed, then the publish operation will fail.

Channels expose a history method providing a means for clients to obtain messages previously sent on the channel. Channel history can be used to return continuous message history up to the exact point a realtime channel was attached.

History provides access to instantaneous “live” history as well as the longer term persisted history for attached channels. If persisted history is enabled for the channel, then messages will typically be stored for 24 – 72 hours. If persisted history is not enabled, Ably retains the last two minutes of message history in memory.

The following example retrieves the first two pages of historical messages published up until the point the channel was attached.

channel.history(function(err, resultPage) { if(err) { console.log('Unable to get channel history; err = ' + err.message); } else { console.log(resultPage.items.length + ' messages received in first page'); if(resultPage.hasNext()) { resultPage.next(function(err, nextPage) { ... }); } } });

See the history documentation for further details of the supported query parameters.

Channels expose a presence member which a client can use to obtain present members and presence event history for the channel itself. See the REST presence documentation for details.

View the Messages API Reference.