Presence API for Angular2 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
- Who, and how many, users are subscribed to a particular channel
- Which channel(s) an individual user is subscribed to
- Associated state information for these users
Learn more about our Presence feature in the Presence overview.
Here now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
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)
Use the following method in the Angular2 SDK:
1hereNow( {Array channels, Array channelGroups, Boolean includeUUIDs, Boolean includeState}, Function callback )
| Parameter | Description |
|---|---|
channelsType: Array Default: n/a | Specifies the channel name to return occupancy results. If channel is not provided, hereNow() will return data for all channels. |
channelGroupsType: Array Default: n/a | The channel group for which here now information should be received. |
includeUUIDsType: Boolean Default: true | Setting uuid to false disables the return of uuids. |
includeStateType: Boolean Default: false | Setting state to true enables the return of subscriber state information. |
callbackType: Function Default: n/a | Executes on a successful/unsuccessful hereNow. |
Sample code
Get a list of uuids subscribed to channel
1pubnub.hereNow(
2 {
3 channels: ["my_channel"],
4 channelGroups : ["my_channelGroup"],
5 includeUUIDs: true,
6 includeState: true
7 },
8 function (status, response) {
9 console.log(status);
10 console.log(response);
11 }
12);
Response
1// Example of Status
2{
3 error: false,
4 operation: "PNHereNowOperation",
5 statusCode: 200
6}
7
8// Example of Response
9{
10 totalChannels: 1,
11 totalOccupancy: 3,
12 channels: {
13 myChannel1: {
14 occupants: [
15 {
show all 35 linesOther examples
Returning state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
1pubnub.hereNow(
2 {
3 channels: ["my_channel"],
4 includeState: true
5 },
6 function (status, response) {
7 // handle status, response
8 }
9);
Example response
1// Example of Status
2{
3 "error": false,
4 "operation": "PNHereNowOperation",
5 "statusCode": 200
6}
7
8// Example of Response
9{
10 "totalChannels": 1,
11 "totalOccupancy": 3,
12 "channels": {
13 "my_channel": {
14 "occupants": [
15 {
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.
To return only occupancy for a channel, set includeUUIDs and includeState to false:
1pubnub.hereNow(
2 {
3 channels: ["my_channel"],
4 includeUUIDs: false,
5 includeState: false
6 },
7 function (status, response) {
8 // handle status, response
9 }
10);
Example response
1// Example of Status
2{
3 "error": false,
4 "operation": "PNHereNowOperation",
5 "statusCode": 200
6}
7
8// Example of Response
9{
10 "totalChannels": 1,
11 "totalOccupancy": 3,
12 "channels": {
13 "my_channel": {
14 "occupants": [],
15 "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.
1pubnub.hereNow(
2 {
3 channelGroups : ["my_channelGroup"]
4 },
5 function (status, response) {
6 // handle status, response
7 }
8);
Example response
1// Example of Status
2{
3 "error": false,
4 "operation": "PNHereNowOperation",
5 "statusCode": 200
6}
7
8// Example of Response
9{
10 "totalChannels": 2,
11 "totalOccupancy": 3,
12 "channels": {
13 "my_channel_1": {
14 "occupants": [
15 {
show all 38 linesWhere now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
This method returns the list of channels a UUID is subscribed to.
Note
If the app restarts (or the page refreshes) within the heartbeat window, no timeout event is generated.
Method(s)
Use the following method in the Angular2 SDK:
1whereNow ({String uuid},Function callback)
| Parameter | Description |
|---|---|
uuidType: String Default: current uuid | Specifies the uuid to return channel list for. |
callbackType: Function Default: n/a | Executes on a successful/unsuccessful whereNow. |
Sample code
You simply need to define the uuid and the callback function to be used to send the data to as in the example below.
Get a list of channels a UUID is subscribed to
1pubnub.whereNow(
2 {
3 uuid: 'my_uuid'
4 },
5 function (status, response) {
6 // handle status, response
7 }
8);
Response
1// Example of Status
2{
3 error: false,
4 operation: "PNWhereNowOperation",
5 statusCode: 200
6}
7
8// Example of Response
9{
10 "channels": ["ch1", "ch2"]
11}
Other examples
Sample code using promises
1pubnub.whereNow({
2 uuid: 'my_uuid'
3}).then((response) => {
4 console.log(response);
5}).catch((error) => {
6 console.log(error);
7});
User state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
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
1setState({Array channels, Array channelGroups, Object state},Function callback)
| Parameter | Description |
|---|---|
channelsType: Array | Either channels or channelGroups should be provided, Specifies the channels to set the state. |
channelGroupsType: Array | Either channels or channelGroups should be provided, Specifies the Channel Group to set the state |
stateType: Object | 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. |
callbackType: Function | Executes on a successful/unsuccessful setState. |
Get state
1getState({String uuid, Arraychannels, ArraychannelGroups},Function callback)
| Parameter | Description |
|---|---|
uuidType: String Default: current uuid | The subscriber uuid to get the current state. |
channelsType: Array Default: n/a | Either channels or channelGroups should be provided, Specifies the channels to get the state. |
channelGroupsType: Array Default: n/a | Either channels or channelGroups should be provided, Specifies the Channel Group to get the state. |
callbackType: Function Default: n/a | Executes on a successful/unsuccessful getState. |
Sample code
Set state
1pubnub.setState(
2 {
3 state: {
4 "Key": "Value"
5 },
6 channels: ["my_channel"]
7 },
8 function (status, response) {
9 // handle status, response
10 }
11);
Get state
1pubnub.getState(
2 {
3 uuid : "my_uuid",
4 channels : ["my_channel"]
5 },
6 function (status, response) {
7 // handle status, response
8 }
9);
Response
Set state
1// Example of Status
2{
3 error: false,
4 operation: "PNSetStateOperation",
5 statusCode: 200
6}
7
8// Example of Response
9{
10 state: {
11 me: 'typing'
12 }
13}
Get state
1// Example of Status
2{
3 error: false,
4 operation: "PNGetStateOperation",
5 statusCode: 200
6}
7
8// Example of Response
9{
10 channels: {
11 ch1: {
12 me: 'typing'
13 }
14 }
15}
Other examples
Set state Basic usage using promises*
1pubnub.setState({
2 state: {
3 "Key": "Value"
4 },
5 channels: ["my_channel"]
6}).then((response) => {
7 console.log(response);
8}).catch((error) => {
9 console.log(error);
10});
Get state Basic usage using promises
1pubnub.getState({
2 uuid : "my_uuid",
3 channels : ["my_channel"]
4}).then((response) => {
5 console.log(response);
6}).catch((error) => {
7 console.log(error);
8});