Presence API for JavaScript SDK
Presence lets you track who is online or offline and store custom state information. Presence shows:
- When a user has joined or left a channel
- How many users are subscribed to a particular channel (occupancy)
- Which channels a user or device is subscribed to
- Presence state associated with these users
- To learn more about Presence as a feature, visit the Presence overview.
Supported and recommended asynchronous patterns
PubNub supports Callbacks, Promises, and Async/Await for asynchronous JS operations. The recommended pattern is Async/Await and all sample requests in this document are based on it. This pattern returns a status only on detecting an error. To receive the error status, you must add the try...catch
syntax to your code.
Here now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
This method returns information about the current state of a channel, including a list of unique user IDs (universally unique identifiers, UUIDs) currently subscribed to the channel and the total occupancy count of the channel.
Cache
This method has a 3-second response cache time.
Method(s)
To call Here Now
, you can use the following method(s) in the JavaScript SDK:
pubnub.hereNow({
channels: Array<string> ,
channelGroups: Array<string> ,
includeUUIDs: boolean ,
includeState: boolean
}); Promise<HereNowResponse>
Parameter | Description |
---|---|
channels Type: array <string> Default: n/a | Specifies the channel name to return occupancy results. You must specify either channels or channelGroups . |
channelGroups Type: array <string> Default: n/a | The channel group for which here now information should be received. You must specify either channels or channelGroups . Wildcards are not supported. |
includeUUIDs Type: boolean Default: true | Setting uuid to false disables the return of uuids. |
includeState Type: boolean Default: false | Setting state to true enables the return of subscriber state information. |
Sample code
Reference code
Get a list of UUIDs subscribed to channel
Response
type hereNowResponse = {
totalChannels: number, // totalChannels = get total of channels
totalOccupancy: number, // totalOccupancy = get total of occupancies
channels: object // channels = get a map with values for each channel with uuids and states for each occupant of the channel
}
Other examples
Returning state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
Example response
// Example of Status
{
"error": false,
"operation": "PNHereNowOperation",
"statusCode": 200
}
// Example of Response
{
"totalChannels": 1,
"totalOccupancy": 3,
"channels": {
"my_channel": {
"occupants": [
{
show all 35 linesReturn occupancy only
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
You can return only the occupancy
information for a single channel by specifying the channel and setting includeUUIDs
and includeState
to false
:
Example response
// Example of Status
{
"error": false,
"operation": "PNHereNowOperation",
"statusCode": 200
}
// Example of Response
{
"totalChannels": 1,
"totalOccupancy": 3,
"channels": {
"my_channel": {
"occupants": [],
"name": "my_channel",
show all 19 linesChannel group usage
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
Example response
// Example of Status
{
"error": false,
"operation": "PNHereNowOperation",
"statusCode": 200
}
// Example of Response
{
"totalChannels": 2,
"totalOccupancy": 3,
"channels": {
"my_channel_1": {
"occupants": [
{
show all 38 linesSample code with promises
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
Where now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
This method returns the list of channels a UUID is subscribed to.
Timeout events
If the app restarts (or the page refreshes) within the heartbeat window, no timeout event is generated.
Method(s)
To call whereNow
, you can use the following method(s) in the JavaScript SDK:
pubnub.whereNow({
uuid: string
}): Promise<WhereNowResponse>
Parameter | Description |
---|---|
uuid Type: string Default: current uuid | Specifies the uuid to return channel list for. |
Sample code
You simply need to define the uuid
to be used to send the data to as in the example below.
Get a list of channels a UUID is subscribed to
Response
// Example of Status
{
error: false,
operation: "PNWhereNowOperation",
statusCode: 200
}
// Example of Response
{
"channels": ["ch1", "ch2"]
}
User state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
Clients can set a dynamic custom state (score, game state, location) for their users on one or more channels and store it on a channel as long as the user stays subscribed.
The state is not persisted, and when the client disconnects, the state data is lost. For more information, refer to Presence State.
Method(s)
Set state
pubnub.setState({
channels: Array<string> ,
channelGroups: Array<string> ,
state: any
}): Promise<SetStateResponse>;
Parameter | Description |
---|---|
channels Type: Array | Either channels or channelGroups should be provided, Specifies the channels to set the state. |
channelGroups Type: Array | Either channels or channelGroups should be provided, Specifies the Channel Group to set the state |
state Type: any | JSON object of key/value pairs with supported data-types of int, float and string. Nesting of key/values is not permitted and key names beginning with prefix pn are reserved. If the state parameter is undefined, the current state for the specified uuid will be returned. If a specified key already exists for the uuid it will be over-written with the new value. Key values can be deleted by setting the particular value to null . |
Get state
pubnub.getState({
uuid: string,
channels: Array<string>,
channelGroups: Array<string>
}): Promise<GetStateResponse>;
Parameter | Description |
---|---|
uuid Type: String Default: current uuid | The subscriber uuid to get the current state. |
channels Type: Array Default: n/a | Either channels or channelGroups should be provided, Specifies the channels to get the state. |
channelGroups Type: Array Default: n/a | Either channels or channelGroups should be provided, Specifies the Channel Group to get the state. |
Sample code
Set state
Get state
Response
Set state
// Example of Status
{
error: false,
operation: "PNSetStateOperation",
statusCode: 200
}
// Example of Response
{
state: {
me: 'typing'
}
}
Get state
// Example of Status
{
error: false,
operation: "PNGetStateOperation",
statusCode: 200
}
// Example of Response
{
channels: {
ch1: {
me: 'typing'
}
}
}