Presence Events

Similar to other Relaybox operations, "presence" relies on the transmission of data via events. In previous sections, we were able to receive data based on event subscriptions and rooms.

Presence extends the core functionality by providing connection identification, allowing subscribers to be aware of who is also part of a room at any given point in time.

Subscribing to Presence Events

To receive presence updates, we first need to subscribe to presence events.

const myRoom = await relayBox.join('myRoom');

await myRoom.presence.subscribe((data) => {
  console.log(data);
});

The code snippet above enables us to receive all presence events for "myRoom". This includes all "join", "leave", and update events.

If we only want to receive a subset of these events, for example, the "join" event, we can specify individual handlers as the first argument.

const myRoom = await relayBox.join('myRoom');

await myRoom.presence.subscribe('join', (data) => {
  // Exclusively handle the "join" event
  console.log(data);
});

await myRoom.presence.subscribe('leave', (data) => {
  // Exclusively handle the "leave" event
  console.log(data);
});

Subscribing to Presence Updates

Alongside the standard "join" and "leave" events, you can also publish a presence "update" event.

Consider a scenario involving a fleet tracking system where a group of vehicles sends periodic location-based data, managed in real-time by a fleet navigation system. Vehicle A joins the presence set at 08:00 AM, sending location data every minute to keep the route finder updated. Presence allows the connection to send additional data as an "update" event, attaching data, such as location coordinates, to the client ID.

const myRoom = await relayBox.join('myRoom');

await myRoom.presence.subscribe('update', (data) => {
  // Exclusively handle the "update" event
  console.log(data);
});

Unsubscribing from Presence Events

You can unsubscribe from presence events using the unsubscribe() method.

// unsubscribe from all presence events for a room
await myRoom.presence.unsubscribe();

// unsubscribe from an individual presence event
await myRoom.presence.unsubscribe('join');

Keeping Track of Presence Members

Each presence event provides information about the connection through the data received with the event. While this data is consistent and up-to-date, it does not account for network issues that might cause a "join" or "leave" event to be received more than once for a given member in a set.

A more robust approach to handling presence members is to use the events as a trigger to fetch an updated list of members. To fetch a complete list of all presence members in a set, we can make use of the "get" method.

const myRoom = relayBox.join('myRoom');

await myRoom.presence.subscribe('join', async (data) => {
  const allMembers = await myRoom.presence.get();

  console.log(allMembers);
});

Calls to the presence get() method do not count towards your quota for events, so feel free to use this method freely without concern for charges.

Additionally, you can fetch the current count of members in a room using the getCount() method.

const myRoom = relayBox.join('myRoom');

await myRoom.presence.subscribe('join', async (data) => {
  const memberCount = await myRoom.presence.getCount();

  console.log(memberCount);
});

    On this page