Skip to main content

Presence API Reference

Participant presence allows clients to monitor and manage the status of other participants currently in the channel. This feature offers robust functionality for tracking and updating participant status.

Usage

To use the presence feature, you need to access the participant property from the channel instance. This property provides methods to update, subscribe, and unsubscribe from presence events.


import { Realtime } from '@superviz/realtime'

const realtime = new Realtime('DEVELOPER_KEY', {
participant: {
id: 'PARTICIPANT_ID',
},
});

const channel = await realtime.connect('YOUR_CHANNEL_NAME');

const participant = channel.participant;

Methods

getAll()

To get a list of all participants' presence information in the channel use the getAll() method, like the following:

const presences = channel.participant.getAll();
console.log(presences);

subscribe()

To receive presence update notifications, you can subscribe to specific events using the subscribe() method. You can use either a string literal or the PresenceEvents enum.

const callback = (event) => {
console.log("Presence update", event.data);
};

channel.participant.subscribe(PresenceEvents.UPDATE, callback);

unsubscribe()

If you no longer want to receive presence update notifications, you can unsubscribe from specific events, using either a string literal or the PresenceEvents enum.

channel.participant.unsubscribe('presence.update');

update()

You can update the participant's presence information using the update method, like the following:

channel.participant.update({ name: "John", status: "online" });

Events

The presence feature emits events to notify you when a participant joins, leaves, or updates their presence information. You can subscribe to these events using the subscribe() method.

JOINED_ROOM

The JOINED_ROOM is dispatched when a participant joins the channel. Here is an example of how to catch the dispatched event:

const callback = (event) => {
console.log("Participant joined", event.data);
};

channel.participant.subscribe('presence.joined-room', callback);

On the callback function, you will receive the following argument:

TypeDescription
PresenceEvent<T>The presence event object that contains the participant's presence data.

LEAVE

The LEAVE event is triggered when a participant leaves the channel. Here is an example of how to catch the dispatched event:

const callback = (event) => {
console.log("Participant left", event.data);
};

channel.participant.subscribe('presence.leave', callback);

On the callback function, you will receive the following argument:

TypeDescription
PresenceEvent<T>The presence event object that contains the participant's presence data.

UPDATE

The UPDATE event is dispatched when a participant's presence data is updated. Here is an example of how to catch the dispatched event:

const callback = (event) => {
console.log("Participant updated", event.data);
};

channel.participant.subscribe('presence.update', callback);

On the callback function, you will receive the following argument:

TypeDescription
PresenceEvent<T>The presence event object that contains the participant's presence data.

Types

PresenceEvent<T>

This is the representation of the data structure of the event dispatched by the presence. It contains the following properties:

FieldTypeDescription
idstringParticipant ID responsible for triggering the event. This value can be nullable, meaning that the event wasn't dispatched by a participant.
namestring or nullParticipant name responsible for triggering the event. This value can be nullable, meaning that the event wasn't dispatched by a participant.
connectionIdstringThe connection ID of the connection used to publish the message. This value can be nullable, meaning that the event wasn't dispatched by a participant.
dataTCustom data associated with the presence event.
timestampnumberThe timestamp of the message, is represented as a numerical value.

PresenceCallback<T>

The PresenceCallback<T> type represents a callback function for presence events.

ParameterTypeDescription
eventPresenceEvent<T>The presence event object

PresenceEvents

Type: enum

The enumerable represents the available presence events. These are the possible values:

EventValueDescription
JOINED_ROOM'presence.joined-room'Triggered when a participant joins the room.
LEAVE'presence.leave'Triggered when a participant leaves the room.
UPDATE'presence.update'Triggered when a participant's presence data is updated.