Presence API for PubNub Go SDK

Presence enables you to track the online and offline status of users and devices in real time and 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 add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

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 Go SDK:

pn.HereNow().
Channels([]string).
ChannelGroups([]string).
IncludeState(bool).
IncludeUUIDs(bool).
QueryParam(queryParam).
Execute()
ParameterTypeRequiredDefaultDescription
Channels[]stringOptionalThe Channels to get the here now details.
ChannelGroups[]stringOptionalThe Channel groups to get the here now details.
IncludeStateboolOptionalFalseIf true, the response will include the presence states of the users for channels/channelGroups.
IncludeUUIDsboolOptionalTrueIf true, the response will include the UUIDs of the connected clients
QueryParammap[string]stringOptionalnilQueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.

Basic Usage

Get a list of UUIDs subscribed to channel

res, status, err := pn.HereNow().
Channels([]string{"my-channel-1"}).
IncludeUUIDs(true).
Execute()

for _, v := range res.Channels {
fmt.Println("---")
fmt.Println("Channel: ", v.ChannelName)
fmt.Println("Occupancy: ", v.Occupancy)
fmt.Println("Occupants")

for _, v := range v.Occupants {
fmt.Println("UUID: ", v.UUID, ", state: ", v.State)
}
}
show all 17 lines

Rest Response from Server

The HereNow() operation returns a. PNHereNowResult which contains the following fields:

MethodTypeDescription
TotalChannelsintTotal Channels
TotalOccupancyintTotal Occupancy
Channels[]HereNowChannelData

HereNowChannelData

MethodTypeDescription
ChannelNamestringChannel name
OccupancyintOccupancy of the Channel
Occupants[]HereNowOccupantsData

HereNowOccupantsData

MethodTypeDescription
UUIDstringUUID of the user
Statemap[string]interfaceState of the user.

Other Examples

Returning State

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

res, status, err := pn.HereNow().
Channels([]string{"my-channel-1"}). // who is present on those channels?
IncludeUUIDs(true). // if false, only shows occupancy count
IncludeState(true). // include state with request (false by default)
Execute()
Example Response
for _, v := range res.Channels {
fmt.Println(v.ChannelName) // my_channel
fmt.Println(v.Occupancy) // 3
fmt.Println(v.Occupants) // members of a channel

for _, v := range v.Occupants {
fmt.Println(v.UUID) // some_uuid;
fmt.Println(v.State) // channel member state, if applicable
}
}

Return Occupancy Only

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

You can return only the occupancy information for a single channel by specifying the channel and setting UUIDs to false:

res, status, err := pn.HereNow().
Channels([]string{"my-channel-1"}). // who is present on those channels?
IncludeUUIDs(false). // if false, only shows occupancy count
IncludeState(false). // include state with request (false by default)
Execute()
Example Response
for _, v := range res.Channels {
fmt.Println(v.ChannelName) // my_channel
fmt.Println(v.Occupancy) // 3
}

Here Now for Channel Groups

res, status, err := pn.HereNow().
ChannelGroups([]string{"cg1", "cg2", "cg3"}). // who is present on channel groups?
IncludeUUIDs(true). // if false, only shows occupancy count
IncludeState(true). // include state with request (false by default)
Execute()
Example Response
res.TotalOccupancy // 12

Where Now

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

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.

Timeout events

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 Go SDK:

pn.WhereNow().
UUID(string).
QueryParam(queryParam).
Execute()
ParameterTypeRequiredDefaultDescription
UUIDstringOptionalUUID to get info on.
QueryParammap[string]stringOptionalnilQueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.

Basic Usage

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

res, status, err := pn.WhereNow().
UUID("username-uuid"). // uuid of the user we want to spy on
Execute()

Rest Response from Server

MethodTypeDescription
Channels[]stringThe 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.

res, status, err := pn.WhereNow().Execute()

User State

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

The state API is used to set/get key/value pairs specific to a subscriber UUID.

State information is supplied as a JSON object of key/value pairs.

Method(s)

Set State

pn.SetState().
Channels([]string).
ChannelGroups([]string).
State(map[string]interface{}).
UUID(string).
QueryParam(queryParam).
Execute()
ParameterTypeRequiredDescription
Channels[]stringOptionalchannels to set state.
ChannelGroups[]stringOptionalchannel groups to set state.
Statemap[string]interfaceOptionalstate to set.
UUIDstringOptionalSet the Presence state info for a given UUID.
QueryParammap[string]stringOptionalQueryParam 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

pn.GetState().
Channels([]string).
ChannelGroups([]string).
UUID(string).
QueryParam(queryParam).
Execute()
ParameterTypeRequiredDescription
Channels[]stringOptionalchannels to get state.
ChannelGroups[]stringOptionalchannel groups to get state.
UUIDstringOptionalGet the Presence state info for a given UUID.
QueryParammap[string]stringOptionalQueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.

Basic Usage

Set State

res, status, err := pn.SetState().
Channels([]string{"ch"}).
State(map[string]interface{}{
"is_typing": true,
}).
Execute()

fmt.Println(res, status, err)

Get State

res, status, err := pn.GetState().
Channels([]string{"ch1", "ch2", "ch3"}).
ChannelGroups([]string{"cg1", "cg2", "cg3"}).
UUID("suchUUID").
Execute()

fmt.Println(res, status, err)

Response

The SetState() operation returns a PNSetStateResult which contains the following fields:

MethodTypeDescription
Stateinterfacemap of UUIDs and the user states.

The GetState() operation returns a PNGetStateResult which contains the following fields:

MethodTypeDescription
Statemap[string]interfacemap of UUIDs and the user states.

Other Examples

Set state for channels in a channel group

myState := map[string]interface{}{
"age": 20,
}

pn.SetState().
ChannelGroups([]string{"cg1", "cg2", "cg3"}).
Channels([]string{"ch1", "ch2", "ch3"}).
State(myState).
Execute()
if presence.Event == "state-change" {
res, status, err := pn.GetState().
Channels([]string{"ch1"}).
UUID("is_typing").
Execute()
fmt.Println(res, status, err)
}

Heartbeat without subscription

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

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:

pn.Presence().
Connected(bool).
Channels([]string).
ChannelGroups([]string).
Execute()
ParameterTypeRequiredDescription
ConnectedboolYesWhether client's presence should be changed to connected (true to join) or off-line (false to leave).
Channels[]stringOptionalList of channel names for which client's presence state should be changed according to passed connected value.
ChannelGroups[]stringOptionalList of channel groups names for which client's presence state should be changed according to passed connected value.

Basic Usage

pn.Presence().
Connected(true).
Channels([]string{"my-channel"}).
ChannelGroups([]string{"my-channel-group"}).
Execute()

Other Examples

To start heartbeating without subscription to channel or channel group

pn.Presence().
Connected(true).
Channels([]string{"my-channel"}).
ChannelGroups([]string{"my-channel-group"}).
Execute()

To stop heartbeating without subscription to channel or channel group

pn.Presence().
Connected(false).
Channels([]string{"my-channel"}).
ChannelGroups([]string{"my-channel-group"}).
Execute()
Last updated on