PresenceMessage
A PresenceMessage represents an individual presence event sent to or received from Ably. Presence messages are delivered through a channel's presence.subscribe() method, or retrieved via presence.get() and presence.history().
1
2
3
await channel.presence.subscribe((presenceMessage) => {
console.log(presenceMessage.action, presenceMessage.clientId);
});Properties
The PresenceMessage type has the following properties:
actionPresenceActionclientIdStringconnectionIdStringdataString, JSON Object, or BufferencodingStringdata payload.extrasJSON Objectextras are the push, ref and privileged objects.idStringPresenceMessage by Ably.timestampIntegerCreate a PresenceMessage from an encoded object
PresenceMessage.fromEncoded(JsonObject: any, channelOptions?: ChannelOptions): Promise<PresenceMessage>A static factory method to create a PresenceMessage from a deserialized PresenceMessage-like object encoded using Ably's wire protocol. The returned promise resolves with the decoded presence message.
1
const presenceMessage = await Ably.Realtime.PresenceMessage.fromEncoded(encodedMsg);Parameters
The PresenceMessage.fromEncoded() method takes the following parameters:
JsonObjectrequiredAnyPresenceMessage-like deserialized object.Returns
Promise<PresenceMessage>
Returns a promise. The promise is fulfilled with a PresenceMessage object decoded from the supplied object, or rejected with an ErrorInfo object if the object cannot be decoded.
Create an array of PresenceMessages from encoded objects
PresenceMessage.fromEncodedArray(JsonArray: any[], channelOptions?: ChannelOptions): Promise<PresenceMessage[]>A static factory method to create an array of PresenceMessage objects from an array of deserialized PresenceMessage-like objects encoded using Ably's wire protocol. The returned promise resolves with the decoded presence messages.
1
const presenceMessages = await Ably.Realtime.PresenceMessage.fromEncodedArray(encodedMsgs);Parameters
The PresenceMessage.fromEncodedArray() method takes the following parameters:
JsonArrayrequiredAny[]PresenceMessage-like deserialized objects.Returns
Promise<PresenceMessage[]>
Returns a promise. The promise is fulfilled with an array of PresenceMessage objects decoded from the supplied array, or rejected with an ErrorInfo object if the array cannot be decoded.
Create a PresenceMessage from values
PresenceMessage.fromValues(values: Partial<Pick<PresenceMessage, 'clientId' | 'data' | 'extras'>>): PresenceMessageA static factory method that initialises a PresenceMessage from a PresenceMessage-like object. Only the clientId, data, and extras fields are used.
1
2
3
4
const presenceMessage = Ably.Realtime.PresenceMessage.fromValues({
clientId: 'user-123',
data: { status: 'available' }
});Parameters
The PresenceMessage.fromValues() method takes the following parameters:
valuesrequiredObjectPresenceMessage-like object to initialise from. Only the clientId, data, and extras fields are accepted.Returns
PresenceMessage
Returns a PresenceMessage object initialised from the supplied values.