deepstream.io is currently under reconstruction for the V4 release!

For V3 and enterprise documentation please go to https://deepstreamhub.com

To continue click here

Presence

Presence allows clients to know when other clients come online and offline, as well as the ability to query for connected clients.

It’s worth mentioning that presence only shows clients that logged in with a username. For example, client.login() won’t trigger the callback client.subscribe(callback).

Methods

client.subscribe( callback )

Argument Type Optional Description
callback Function false Will be invoked with the username of a client, and a boolean to indicate if it was a login(true) or logout(false) event

Subscribes to presence events. Callback will receive the username of the newly added client

// Client A
client.presence.subscribe((username, isLoggedIn) => {
  // handle new user
})

// Client B
client.login({username: 'Alex'})

client.unsubscribe( callback )

Argument Type Optional Description
callback Function false A previously registered callback

Removes a previously registered presence callback

function onOnlineStatusChange( username, isOnline ) {
  //...
}
// Client A
client.presence.subscribe( onOnlineStatusChange )
client.presence.unsubscribe( onOnlineStatusChange )

client.getAll( callback )

Argument Type Optional Description
callback Function false A function that will be called with an array of usernames that are online

Queries for currently connected clients

// Client B
client.presence.getAll((clients) => {
    // [ 'Alex', 'Ben', 'Charlie' ]
})