Go V4 Presence API Reference for Realtime Apps
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 add-onRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
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.
Method(s)
To call Here Now
you can use the following method(s) in the Go V4 SDK:
pn.HereNow().Channels([]string).ChannelGroups([]string).IncludeState(bool).IncludeUUIDs(bool).QueryParam(queryParam).Execute()
Parameter Type Required Default Description Channels
[]string Optional The Channels
to get the here now details.ChannelGroups
[]string Optional The Channel groups
to get the here now details.IncludeState
bool Optional False If true
, the response will include the presence states of the users forchannels
/channelGroups
.IncludeUUIDs
bool Optional True If true
, the response will include theUUIDs
of the connected clientsQueryParam
map[string]string Optional 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.
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)
}
}
fmt.Println(status, err)
Rest Response from Server
The HereNow()
operation returns a. PNHereNowResult
which contains the following fields:
Method | Type | Description |
---|---|---|
TotalChannels | int | Total Channels |
TotalOccupancy | int | Total Occupancy |
Channels | []HereNowChannelData |
Method | Type | Description |
---|---|---|
ChannelName | string | Channel name |
Occupancy | int | Occupancy of the Channel |
Occupants | []HereNowOccupantsData |
Method | Type | Description |
---|---|---|
UUID | string | UUID of the user |
State | map[string]interface{} | State of the user. |
Other Examples
- Returning State: Requires Presence add-onRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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()
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-onRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-You can return only the
occupancy
information for a single channel by specifying the channel and settingUUIDs
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()
for _, v := range res.Channels { fmt.Println(v.ChannelName) // my_channel fmt.Println(v.Occupancy) // 3 }
- Returning UUIDs and occupancy for all channels: Requires Presence add-onRequires that the Presence add-on is enabled with Global Here Now checked for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-You can return the list of
UUIDs
andoccupancy
for allchannels
by omitting the channel:res, status, err := pn.HereNow(). IncludeUUIDs(true). // if false, only shows occupancy count IncludeState(false). // include state with request (false by default) Execute()
res.TotalChannels // 4 res.TotalOccupancy // 12 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; } }
- Return Occupancy for all channels: Requires Presence add-onRequires that the Presence add-on is enabled with Global Here Now checked for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-You can return only the
occupancy
information(Global Here Now)
by omitting thechannel name
res, status, err := pn.HereNow(). IncludeUUIDs(true). // if false, only shows occupancy count IncludeState(true). // include state with request (false by default) Execute()
res.TotalChannels // 4 res.TotalOccupancy // 12 for _, v := range res.Channels { fmt.Println(v.Occupancy) // 3 fmt.Println(v.Occupants) // members of a channel for _, v := range v.Occupants { fmt.Println(v.UUID) // some_uuid; } }
-
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()
res.TotalOccupancy // 12
Where Now
Requires Presence add-onRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
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 Go V4 SDK:
pn.WhereNow().UUID(string).QueryParam(queryParam).Execute()
Parameter Type Required Default Description UUID
string Optional UUID
to get info on.QueryParam
map[string]string Optional 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.
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
Method | Type | Description |
---|---|---|
Channels | []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.
res, status, err := pn.WhereNow().Execute()
User State
Requires Presence add-onRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
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)
-
pn.SetState().Channels([]string).ChannelGroups([]string).State(map[string]interface{}).UUID(string).QueryParam(queryParam).Execute()
Parameter Type Required Description Channels
[]string Optional channels
to setstate
.ChannelGroups
[]string Optional channel groups
to setstate
.State
map[string]interface{} Optional state
to set.UUID
string Optional Set the Presence state info for a given UUID
.QueryParam
map[string]string Optional 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. -
pn.GetState().Channels([]string).ChannelGroups([]string).UUID(string).QueryParam(queryParam).Execute()
Basic Usage
res, status, err := pn.SetState().
Channels([]string{"ch"}).
State(map[string]interface{}{
"is_typing": true,
}).
Execute()
fmt.Println(res, status, err)
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:
Method | Type | Description |
---|---|---|
State | interface{} | map of UUIDs and the user states. |
The GetState()
operation returns a PNGetStateResult
which contains the following fields:
Method | Type | Description |
---|---|---|
State | map[string]interface{} | map 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-onRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
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 V4 SDK:
pn.Presence().Connected(bool).Channels([]string).ChannelGroups([]string).Execute()
Parameter Type Required Description Connected
bool Yes Whether client's presence should be changed to connected ( true
to join) or off-line (false
to leave).Channels
[]string Optional List of channel names for which client's presence state should be changed according to passed connected value. ChannelGroups
[]string Optional List 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()