Presence API for Vue SDK
Presence enables you to track the online and offline status of users and devices in real time, as well as store custom state information. Presence provides authoritative information on:
- 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 here.
Here now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
You can obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count of the channel by calling the hereNow() function in your application.
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 Vue 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
1import PubNubVue from 'pubnub-vue';
2
3const pubnub = PubNubVue.getInstance();
4
5pubnub.hereNow(
6 {
7 channels: ["ch1"],
8 channelGroups : ["cg1"],
9 includeUUIDs: true,
10 includeState: true
11 },
12 function(status, response) {
13 // handle status, response
14 }
15);
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
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
1import PubNubVue from 'pubnub-vue';
2
3const pubnub = PubNubVue.getInstance();
4
5pubnub.hereNow(
6 {
7 channels: ["my_channel"],
8 includeState: true
9 },
10 function(status, response) {
11 // handle status, response
12 }
13);
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.
You can return only the occupancy information for a single channel by specifying the channel and setting UUIDs to false:
1import PubNubVue from 'pubnub-vue';
2
3const pubnub = PubNubVue.getInstance();
4
5pubnub.hereNow(
6 {
7 channels: ["my_channel"],
8 includeUUIDs: false
9 },
10 function(status, response) {
11 // handle status, response
12 }
13);
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.
1import PubNubVue from 'pubnub-vue';
2
3const pubnub = PubNubVue.getInstance();
4
5pubnub.hereNow(
6 {
7 channelGroups : ['my_channel_group']
8 },
9 function(status, response) {
10 // handle status, response
11 }
12);
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 linesSample code using promises
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
1import PubNubVue from 'pubnub-vue';
2
3const pubnub = PubNubVue.getInstance();
4
5pubnub.hereNow({
6 channels: ["ch1"],
7 channelGroups: ["cg1"],
8 includeUUIDs: true,
9 includeState: true
10}).then((response) => {
11 console.log(response);
12}).catch((error) => {
13 console.log(error);
14});
Returning state using promises
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
1import PubNubVue from 'pubnub-vue';
2
3const pubnub = PubNubVue.getInstance();
4
5pubnub.hereNow({
6 channels: ["my_channel"],
7 includeState: true
8}).then((response) => {
9 console.log(response);
10}).catch((error) => {
11 console.log(error);
12});
Return occupancy only using promises
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
You can return only the occupancy information for a single channel by specifying the channel and setting uuids to false:
1import PubNubVue from 'pubnub-vue';
2
3const pubnub = PubNubVue.getInstance();
4
5pubnub.hereNow({
6 channels: ["my_channel"],
7 includeUUIDs: false
8}).then((response) => {
9 console.log(response);
10}).catch((error) => {
11 console.log(error);
12});
Channel group usage using promises
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
1import PubNubVue from 'pubnub-vue';
2
3const pubnub = PubNubVue.getInstance();
4
5pubnub.hereNow({
6 channelGroups: ["my_channel_group"]
7}).then((response) => {
8 console.log(response);
9}).catch((error) => {
10 console.log(error);
11});
Where now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
Description
You can obtain information about the current list of channels to which a UUID is subscribed to by calling the whereNow function in your application.
Note
If the app is killed/crashes and restarted (or the page containing the PubNub instance is refreshed on the browser) within the heartbeat window no timeout event is generated.
Method(s)
To call whereNow you can use the following method(s) in the Vue 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. |