Message
A Message represents an individual message that is sent to or received from Ably. Messages are delivered through a channel's subscribe() method or retrieved via history().
1
2
3
channel.subscribe((message) => {
console.log(message.name, message.data);
});Properties
The Message interface has the following properties:
nameStringdataString, JSON Object, or BufferextrasJSON Objectpush, headers (a map of strings to strings for arbitrary customer-supplied metadata), ephemeral, and privileged.idStringclientIdStringconnectionIdStringconnectionKeyStringtimestampIntegerencodingStringdata payload.actionMessageActionserialStringannotationsMessageAnnotationsversionMessageVersionCreate 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.
1
const message = await Ably.Realtime.Message.fromEncoded(encodedMsg);Parameters
The Message.fromEncoded() method takes the following parameters:
JsonObjectrequiredAnyMessage-like deserialized object.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.
1
const messages = await Ably.Realtime.Message.fromEncodedArray(encodedMsgs);Parameters
The Message.fromEncodedArray() method takes the following parameters:
JsonArrayrequiredAny[]Message-like deserialized objects.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.