# Presence The `Presence` interface provides methods for tracking which users are currently in a chat room. Access it via `room.presence`. #### Javascript ``` const presence = room.presence; ``` ## Enter presence `presence.enter(data?: PresenceData): Promise` Enters the current user into the chat room presence set. This notifies other users that you have joined the room. The room must be [attached](https://ably.com/docs/chat/api/javascript/room.md#attach) before calling this method. ### Javascript ``` await room.presence.enter({ status: 'online', nickname: 'Alice' }); ``` ### Parameters The `enter()` method takes the following parameters: | Parameter | Required | Description | Type | | --- | --- | --- | --- | | data | Optional | JSON-serializable data to associate with the user's presence. |
|
| Property | Description | Type | | --- | --- | --- | | | JSON-serializable data that can be associated with a user's presence in a room. | JsonObject |
### Returns `Promise` Returns a promise. The promise is fulfilled when the user has successfully entered the presence set, or rejected with an [`ErrorInfo`](https://ably.com/docs/chat/api/javascript/chat-client.md#errorinfo) object. ## Update presence data `presence.update(data?: PresenceData): Promise` Updates the presence data for the current user in the chat room. Use this to change your status or other presence information without leaving and re-entering. The room must be [attached](https://ably.com/docs/chat/api/javascript/room.md#attach) before calling this method. ### Javascript ``` await room.presence.update({ status: 'away', nickname: 'Alice' }); ``` ### Parameters The `update()` method takes the following parameters: | Parameter | Required | Description | Type | | --- | --- | --- | --- | | data | Optional | JSON-serializable data to replace the user's current presence data. |
|
### Returns `Promise` Returns a promise. The promise is fulfilled when the presence data has been updated, or rejected with an [`ErrorInfo`](https://ably.com/docs/chat/api/javascript/chat-client.md#errorinfo) object. ## Leave presence `presence.leave(data?: PresenceData): Promise` Removes the current user from the chat room presence set. This notifies other users that you have left the room. The room must be [attached](https://ably.com/docs/chat/api/javascript/room.md#attach) before calling this method. ### Javascript ``` await room.presence.leave({ status: 'offline' }); ``` ### Parameters The `leave()` method takes the following parameters: | Parameter | Required | Description | Type | | --- | --- | --- | --- | | data | Optional | Final presence data to include with the leave event. |
|
### Returns `Promise` Returns a promise. The promise is fulfilled when the user has left the presence set, or rejected with an [`ErrorInfo`](https://ably.com/docs/chat/api/javascript/chat-client.md#errorinfo) object. ## Get current presence members `presence.get(params?: RealtimePresenceParams): Promise` Retrieves the current members present in the chat room. The room must be [attached](https://ably.com/docs/chat/api/javascript/room.md#attach) before calling this method. ### Javascript ``` const members = await room.presence.get(); members.forEach(member => { console.log(`${member.clientId} is ${member.data?.status}`); }); ``` ### Parameters The `get()` method takes the following parameters: | Parameter | Required | Description | Type | | --- | --- | --- | --- | | params | Optional | Parameters to filter the presence set. |
|
| Property | Required | Description | Type | | --- | --- | --- | --- | | clientId | Optional | Filters the returned presence members by a specific client ID. | String | | connectionId | Optional | Filters the returned presence members by a specific connection ID. | String | | waitForSync | Optional | Whether to wait for a full presence set synchronization before returning results. Default `true`. | Boolean |
### Returns `Promise` Returns a promise. The promise is fulfilled with an array of presence members currently in the room, or rejected with an [`ErrorInfo`](https://ably.com/docs/chat/api/javascript/chat-client.md#errorinfo) object. ## Check if a user is present `presence.isUserPresent(clientId: string): Promise` Checks whether a specific user is currently present in the chat room. The room must be [attached](https://ably.com/docs/chat/api/javascript/room.md#attach) before calling this method. ### Javascript ``` const isAliceHere = await room.presence.isUserPresent('alice-123'); console.log('Alice is present:', isAliceHere); ``` ### Parameters The `isUserPresent()` method takes the following parameters: | Parameter | Required | Description | Type | | --- | --- | --- | --- | | clientId | Required | The client ID of the user to check. | String |
### Returns `Promise` Returns a promise. The promise is fulfilled with `true` if the user is present or `false` otherwise, or rejected with an [`ErrorInfo`](https://ably.com/docs/chat/api/javascript/chat-client.md#errorinfo) object. ## Subscribe to presence events `presence.subscribe(listener: PresenceListener): Subscription` Subscribes to all presence events in the chat room. Receive notifications when users enter, leave, or update their presence data. ### Javascript ``` import { PresenceEventType } from '@ably/chat'; const { unsubscribe } = room.presence.subscribe((event) => { const member = event.member; switch (event.type) { case PresenceEventType.Enter: console.log(`${member.clientId} joined`); break; case PresenceEventType.Leave: console.log(`${member.clientId} left`); break; case PresenceEventType.Update: console.log(`${member.clientId} updated their status`); break; case PresenceEventType.Present: console.log(`${member.clientId} is present`); break; } }); // To stop receiving presence events unsubscribe(); ``` ### Parameters The `subscribe()` method takes the following parameters: | Parameter | Required | Description | Type | | --- | --- | --- | --- | | listener | Required | Callback function invoked when any presence event occurs. |
|
| Property | Description | Type | | --- | --- | --- | | type | The type of presence event. |
| | member | The presence member associated with this event. |
|
| Value | Description | | --- | --- | | Enter | A user has entered the presence set. The value is `enter`. | | Leave | A user has left the presence set. The value is `leave`. | | Update | A user has updated their presence data. The value is `update`. | | Present | Indicates a user is present (received during initial sync). The value is `present`. |
| Property | Description | Type | | --- | --- | --- | | clientId | The client ID of the present user. | String | | connectionId | The connection ID of the present user. | String | | data | The presence data associated with the user. |
or Undefined | | extras | Additional data included with the presence message. | JsonObject or Undefined | | updatedAt | When this presence state was last updated. | Date |
### Returns `Subscription` Returns an object with the following methods: #### Unsubscribe from presence events `unsubscribe(): void` Call `unsubscribe()` to stop receiving presence events. ## Example ### Javascript ``` const room = await chatClient.rooms.get('my-room'); await room.attach(); // Subscribe to presence events const { unsubscribe } = room.presence.subscribe((event) => { const member = event.member; console.log(`${event.type}: ${member.clientId}`); if (member.data) { console.log('Data:', member.data); } }); // Enter the room with custom data await room.presence.enter({ status: 'online', nickname: 'Alice', avatar: 'https://example.com/alice.png' }); // Get everyone currently in the room const members = await room.presence.get(); console.log(`${members.length} users in room`); // Update your status await room.presence.update({ status: 'away' }); // Check if a specific user is present const isBobHere = await room.presence.isUserPresent('bob-456'); // Leave when done await room.presence.leave(); unsubscribe(); ```