Message

A Message represents an individual message that is sent to or received from Ably. Messages are published through a channel's publish() method and retrieved via history().

JavaScript

1

2

3

const result = await channel.history();
const message = result.items[0];
console.log(message.name, message.data);

Properties

The Message interface has the following properties:

nameString
The event name, if provided.
dataString, JSON Object, or Buffer
The message payload, if provided.
extrasJSON Object
Metadata and/or ancillary payloads, if provided. Valid payloads include push, headers (a map of strings to strings for arbitrary customer-supplied metadata), ephemeral, and privileged.
idString
A Unique ID assigned by Ably to this message.
clientIdString
The client ID of the publisher of this message.
connectionIdString
The connection ID of the publisher of this message.
connectionKeyString
A connection key, which can optionally be included for a REST publish as part of the publishing on behalf of a realtime client functionality.
timestampInteger
Timestamp when the message was first received by the Ably, as milliseconds since the epoch.
encodingString
This will typically be empty as all messages received from Ably are automatically decoded client-side using this value. However, if the message encoding cannot be processed, this attribute will contain the remaining transformations not applied to the data payload.
actionMessageAction
The action type of the message.
serialString
A server-assigned identifier that will be the same in all future updates of this message. It can be used to add annotations to a message or to update or delete it. Serial will only be set if you enable annotations, updates, deletes, and appends in rules.
annotationsMessageAnnotations
An object containing information about annotations that have been made to the object.
versionMessageVersion
An object containing version metadata for messages that have been updated or deleted.

Create a message from an encoded object

Message.fromEncoded(JsonObject: any, channelOptions?: ChannelOptions): Promise<InboundMessage>

A static factory method to create a Message from a deserialized Message-like object encoded using Ably's wire protocol. The returned promise resolves with the decoded message.

JavaScript

1

const message = await Ably.Rest.Message.fromEncoded(encodedMsg);

Parameters

The Message.fromEncoded() method takes the following parameters:

JsonObjectrequiredAny
A Message-like deserialized object.
channelOptionsoptionalChannelOptions
If you have an encrypted channel, use this to allow the SDK to decrypt the data.

Returns

Promise<InboundMessage>

Returns a promise. The promise is fulfilled with the decoded message, typed InboundMessage: a Message whose id, timestamp, action, version, and annotations fields are guaranteed to be present. The promise is rejected with an ErrorInfo object if the object cannot be decoded.

Create messages from an encoded array

Message.fromEncodedArray(JsonArray: any[], channelOptions?: ChannelOptions): Promise<InboundMessage[]>

A static factory method to create an array of Message objects from an array of deserialized Message-like objects encoded using Ably's wire protocol. The returned promise resolves with the decoded messages.

JavaScript

1

const messages = await Ably.Rest.Message.fromEncodedArray(encodedMsgs);

Parameters

The Message.fromEncodedArray() method takes the following parameters:

JsonArrayrequiredAny[]
An array of Message-like deserialized objects.
channelOptionsoptionalChannelOptions
If you have an encrypted channel, use this to allow the SDK to decrypt the data.

Returns

Promise<InboundMessage[]>

Returns a promise. The promise is fulfilled with an array of decoded messages, typed InboundMessage as for fromEncoded(). The promise is rejected with an ErrorInfo object if the array cannot be decoded.