Presence
Methods
get
Get the current presence member set for this channel. In the REST client library this method directly queries Ably's REST presence API
get(Object params?): Promise<PresenceMessage[]>
Gets an array of members present on the channel as PresenceMessage objects.
Parameters
| Name | Description |
|---|---|
| params | an optional object containing query parameters as specified below. |
params parameters
| Name | Description |
|---|---|
| clientId | when provided, will filter array of members returned that match the provided clientId string |
| connectionId | when provided, will filter array of members returned that match the provided connectionId string |
Returns
Returns a promise. On success, the promise is fulfilled with an array of PresenceMessage objects corresponding to the current set of present members on the channel. On failure, the promise is rejected with an ErrorInfo object that details the reason why it was rejected.
history
history(Object params?): Promise<PaginatedResult<PresenceMessage>>
Gets a paginated set of historical presence message events for this channel. If the channel is configured to persist messages to disk, then the presence message event history will typically be available for 24 - 72 hours. If not, presence message events are only retained in memory by the Ably service for two minutes.
Parameters
| Parameter | Description | Type |
|---|---|---|
| params | an optional object containing query parameters, as specified in the presence history API documentation. | Object |
Returns
Returns a promise. On success, the promise is fulfilled with a PaginatedResult object containing an array of PresenceMessage objects corresponding to the current page of results. On failure, the promise is rejected with an ErrorInfo object that details the reason why it was rejected.
Related types
PresenceMessage
A PresenceMessage represents an individual presence update that is sent to or received from Ably.
Properties
| Property | Description | Type |
|---|---|---|
| action | the event signified by a PresenceMessage. See Presence action | int enum { ABSENT, PRESENT, ENTER, LEAVE, UPDATE } |
| data | The presence update payload, if provided | String, StringBuffer, JSON Object |
| extras | Metadata and/or ancillary payloads, if provided. The only currently valid payloads for extras are the push, ref and privileged objects. | JSON Object |
| id | Unique ID assigned by Ably to this presence update | String |
| clientId | The client ID of the publisher of this presence update | String |
| connectionId | The connection ID of the publisher of this presence update | String |
| timestamp | Timestamp when the presence update was received by Ably, as milliseconds since the epoch. | Integer |
| encoding | This will typically be empty as all presence updates 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 | String |
PresenceMessage constructors
PresenceMessage.fromEncoded
PresenceMessage.fromEncoded(Object encodedPresMsg, ChannelOptions channelOptions?) -> PresenceMessage
A static factory method to create a PresenceMessage from a deserialized PresenceMessage-like object encoded using Ably's wire protocol.
Parameters
| Parameter | Description | Type |
|---|---|---|
| encodedPresMsg | a PresenceMessage-like deserialized object. | Object |
| channelOptions | an optional ChannelOptions. If you have an encrypted channel, use this to allow the library to decrypt the data. | Object |
Returns
A PresenceMessage object
PresenceMessage.fromEncodedArray
PresenceMessage.fromEncodedArray(Object[] encodedPresMsgs, ChannelOptions channelOptions?) -> PresenceMessage[]
A static factory method to create an array of PresenceMessages from an array of deserialized PresenceMessage-like object encoded using Ably's wire protocol.
Parameters
| Parameter | Description | Type |
|---|---|---|
| encodedPresMsgs | an array of PresenceMessage-like deserialized objects. | Array |
| channelOptions | an optional ChannelOptions. If you have an encrypted channel, use this to allow the library to decrypt the data. | Object |
Returns
An Array of PresenceMessage objects
Presence action
Presence action is a String with a value matching any of the Realtime Presence states & events.
1
2
3
4
5
6
7
var PresenceActions = [
'absent', // (reserved for internal use)
'present',
'enter',
'leave',
'update'
]PaginatedResult
A PaginatedResult is a type that represents a page of results for all message and presence history, stats and REST presence requests. The response from a Ably REST API paginated query is accompanied by metadata that indicates the relative queries available to the PaginatedResult object.
Properties
| Property | Description | Type |
|---|---|---|
| items | contains the current page of results (for example an Array of Message or PresenceMessage objects for a channel history request) | Array <Message, Presence, Stats> |
Methods
first
first(): Promise<PaginatedResult>
Returns a promise. On success, the promise is fulfilled with a new PaginatedResult for the first page of results. On failure, the promise is rejected with an ErrorInfo object that details the reason why it was rejected.
hasNext
Boolean hasNext()
Returns true if there are more pages available by calling next and returns false if this page is the last page available.
isLast
Boolean isLast()
Returns true if this page is the last page and returns false if there are more pages available by calling next available.
next
next(): Promise<PaginatedResult | null>
Returns a promise. On success, the promise is fulfilled with a new PaginatedResult loaded with the next page of results. If there are no further pages, then null is returned. On failure, the promise is rejected with an ErrorInfo object that details the reason why it was rejected.
current
current(): Promise<PaginatedResult>
Returns a promise. On success, the promise is fulfilled with a new PaginatedResult loaded with the current page of results. On failure, the promise is rejected with an ErrorInfo object that details the reason why it was rejected.
Example
1
2
3
4
5
const paginatedResult = await channel.history();
console.log('Page 0 item 0:' + paginatedResult.items[0].data);
const nextPage = await paginatedResult.next();
console.log('Page 1 item 1: ' + nextPage.items[1].data);
console.log('Last page?: ' + nextPage.isLast());