Presence

A Presence object enables a client to query the presence set of a channel over REST. Access it via the presence property of a Channel instance.

The REST presence interface is query-only: it retrieves the current members of the presence set and their history. Entering, updating, leaving, and subscribing to presence events require the realtime SDK's RealtimePresence interface.

JavaScript

1

2

const channel = rest.channels.get('all-gas-ion');
const members = await channel.presence.get();

Get the presence set

presence.get(params?: RestPresenceParams): Promise<PaginatedResult<PresenceMessage>>

Retrieve the current members of the presence set for the channel. This method directly queries Ably's REST presence API and returns a paginated set of PresenceMessage objects, one for each member, with metadata such as the member's clientId, connectionId, action, and any associated data.

JavaScript

1

2

3

4

const members = await channel.presence.get({ limit: 100 });
members.items.forEach((member) => {
  console.log(member.clientId, member.data);
});

Parameters

The get() method takes the following parameters:

paramsoptionalRestPresenceParams
Filter options for the query.

Returns

Promise<PaginatedResult<PresenceMessage>>

Returns a promise. The promise is fulfilled with a PaginatedResult containing an array of PresenceMessage objects representing the current members of the presence set, or rejected with an ErrorInfo object.

Get presence history

presence.history(params?: RestHistoryParams): Promise<PaginatedResult<PresenceMessage>>

Get a paginated set of historical presence message events for the channel. If the channel is configured to persist messages, presence message history is available for the duration configured for your account. Otherwise, presence message events are only retained in memory by the Ably service for two minutes.

JavaScript

1

2

3

4

const history = await channel.presence.history({ limit: 50 });
history.items.forEach((member) => {
  console.log(member.action, member.clientId);
});

Parameters

The history() method takes the following parameters:

paramsoptionalRestHistoryParams
Query parameters as specified in the presence history API documentation.

Returns

Promise<PaginatedResult<PresenceMessage>>

Returns a promise. The promise is fulfilled with a PaginatedResult containing an array of PresenceMessage objects, or rejected with an ErrorInfo object.