Presence API for Go 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
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.
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 Go SDK:
1pn.HereNow().
2 Channels([]string).
3 ChannelGroups([]string).
4 IncludeState(bool).
5 IncludeUUIDs(bool).
6 QueryParam(queryParam).
7 Execute()
| Parameter | Description |
|---|---|
ChannelsType: []string Default: n/a | The Channels to get the here now details. |
ChannelGroupsType: []string Default: n/a | The Channel groups to get the here now details. Wildcards are not supported. |
IncludeStateType: bool Default: False | If true, the response will include the presence states of the users for channels/channelGroups. |
IncludeUUIDsType: bool Default: True | If true, the response will include the UUIDs of the connected clients |
QueryParamType: map[string]string Default: nil | QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API. |
Sample code
Reference code
Get a list of UUIDs subscribed to channel
1
Rest response from server
The HereNow() operation returns a. PNHereNowResult which contains the following fields:
| Method | Description |
|---|---|
TotalChannelsType: int | Total Channels |
TotalOccupancyType: int | Total Occupancy |
ChannelsType: []HereNowChannelData |
HereNowChannelData
| Method | Description |
|---|---|
ChannelNameType: string | Channel name |
OccupancyType: int | Occupancy of the Channel |
OccupantsType: []HereNowOccupantsData |
HereNowOccupantsData
| Method | Description |
|---|---|
UUIDType: string | UUID of the user |
StateType: map[string]interface | State of the user. |
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.
1
Return 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 UUIDs to false:
1
Here now for channel groups
1
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 Go SDK:
1pn.WhereNow().
2 UUID(string).
3 QueryParam(queryParam).
4 Execute()
| Parameter | Description |
|---|---|
UUIDType: string Default: n/a | UUID to get info on. |
QueryParamType: map[string]string Default: nil | QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API. |
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
1
Rest response from server
| Method | Description |
|---|---|
ChannelsType: []string | The list of channels where the UUID is present. |
Other examples
In the case you omit UUID field in WhereNow() request, the UUID of a current PubNub instance will be used.
1
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
1pn.SetState().
2 Channels([]string).
3 ChannelGroups([]string).
4 State(map[string]interface{}).
5 UUID(string).
6 QueryParam(queryParam).
7 Execute()
| Parameter | Description |
|---|---|
ChannelsType: []string | channels to set state. |
ChannelGroupsType: []string | channel groups to set state. |
StateType: map[string]interface | state to set. |
UUIDType: string | Set the Presence state info for a given UUID. |
QueryParamType: map[string]string | QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API. |
Get state
1pn.GetState().
2 Channels([]string).
3 ChannelGroups([]string).
4 UUID(string).
5 QueryParam(queryParam).
6 Execute()
| Parameter | Description |
|---|---|
ChannelsType: []string | channels to get state. |
ChannelGroupsType: []string | channel groups to get state. |
UUIDType: string | Get the Presence state info for a given UUID. |
QueryParamType: map[string]string | QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API. |
Sample code
Set state
1
Get state
1
Response
The SetState() operation returns a PNSetStateResult which contains the following fields:
| Method | Description |
|---|---|
StateType: interface | map of UUIDs and the user states. |
The GetState() operation returns a PNGetStateResult which contains the following fields:
| Method | Description |
|---|---|
StateType: map[string]interface | map of UUIDs and the user states. |
Other examples
Set state for channels in a channel group
1
Get state for multiple channels
1
Heartbeat without subscription
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 send presence heartbeat notifications without subscribing to a channel. These notifications are sent periodically and indicate whether a client is connected or not. Make sure to correctly set the presence timeout and interval properties during Configuration This feature requires the Presence add-on.
Method(s)
To send heartbeat notifications without subscribing to a channel, you can use the following method(s) in the Go SDK:
1pn.Presence().
2 Connected(bool).
3 Channels([]string).
4 ChannelGroups([]string).
5 Execute()
| Parameter | Description |
|---|---|
Connected *Type: bool | Whether client's presence should be changed to connected (true to join) or off-line (false to leave). |
ChannelsType: []string | List of channel names for which client's presence state should be changed according to passed connected value. |
ChannelGroupsType: []string | List of channel groups names for which client's presence state should be changed according to passed connected value. |
Sample code
1
Other examples
To stop heartbeating without subscription to channel or channel group
1