JavaScript REST SDK: Presence
A Presence object enables a client to query the presence set of a channel over HTTP. Access it via the presence property of a Channel instance.
The Presence object of an HTTP client 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 RealtimePresence object of a realtime client.
1
2
const channel = pubsubHttpClient.channels.get('arc-one-ego');
const members = await channel.presence.get();Get the presence set
presence.get(params?: HttpPresenceParams): 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.
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:
paramsoptionalHttpPresenceParamsReturns
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?: HttpHistoryParams): 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.
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:
paramsoptionalHttpHistoryParamsReturns
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.