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().

JavaScript

1

2

3

await channel.presence.subscribe((presenceMessage) => {
  console.log(presenceMessage.action, presenceMessage.clientId);
});

Properties

The PresenceMessage type has the following properties:

actionPresenceAction
The type of presence action this message represents.
clientIdString
The client ID of the publisher of this presence message.
connectionIdString
The connection ID of the publisher of this presence message.
dataString, JSON Object, or Buffer
The presence data payload, if provided.
encodingString
This will typically be empty as all presence messages received from Ably are automatically decoded client-side using this value. However, if the encoding cannot be processed, this attribute will contain the remaining transformations not applied to the data payload.
extrasJSON Object
Metadata and/or ancillary payloads, if provided. The only currently valid payloads for extras are the push, ref and privileged objects.
idString
A unique ID assigned to each PresenceMessage by Ably.
timestampInteger
Timestamp when the presence message was first received by Ably, as milliseconds since the epoch.

Create 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.

JavaScript

1

const presenceMessage = await Ably.Realtime.PresenceMessage.fromEncoded(encodedMsg);

Parameters

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

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

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.

JavaScript

1

const presenceMessages = await Ably.Realtime.PresenceMessage.fromEncodedArray(encodedMsgs);

Parameters

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

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

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'>>): PresenceMessage

A static factory method that initialises a PresenceMessage from a PresenceMessage-like object. Only the clientId, data, and extras fields are used.

JavaScript

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:

valuesrequiredObject
A PresenceMessage-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.