Presence enables you to track the online and offline status of users and devices in real time and store custom state information. When you have Presence enabled, PubNub automatically creates a presence channel for each channel. Subscribing to a presence channel or presence channel group will only return presence events.
Call the hereNow() method to obtain information about the current state of a space, including a list of unique users currently subscribed to the space.
Get occupancy of users in a space
Call the hereNow() method to get a count of online users in a space. The count includes unique users who are subscribed to the space.
Receive Presence Events
With PubNub Presence, you can receive realtime events when users connect to or disconnect from spaces. These events can be received either directly on the client or on the server-side using webhooks.
The following presence events are available to you:
join fires when a user subscribes to a space
leave fires when a user unsubscribes from a space
timeout fires when a connection to a space is severed and the subscriber hasn't been seen in 320 seconds (just over 5 minutes). You can customize the timeout interval using the heartbeat and heartbeat interval settings.
state-change fires anytime the user's state is changed using the state API (function signature varies by SDK)
interval fires to provide an occupancy count. The default setting for the Presence Interval property is 10 seconds, which is configurable on your PubNub Dashboard's Presence add-on panel.
Subscribing to presence events
Enable the withPresence flag when you subscribe to spaces to receive presence events.
Once you have subscribed with presence, the listener starts receiving presence events.