PubNub LogoDocs
SupportContact SalesLoginTry Our APIs

›API Reference

go

  • Getting Started
  • API Reference

    • Configuration
    • Publish & Subscribe
    • Presence
    • Access Manager
    • Channel Groups
    • Message Persistence
    • Mobile Push
    • Objects
    • Files
    • Message Actions
    • Miscellaneous
  • Status Events
  • Troubleshooting
  • Change Log
  • Feature Support
  • Platform Support

Presence API for PubNub Go 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 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:

  1. 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)
    }
}

fmt.Println(status, err)

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]interface{}State of the user.

Other Examples

  1. 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()
    

    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
        }
    }
    
  2. 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 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
    }
    
  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-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:

  1. 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-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)

  1. 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]interface{}Optionalstate 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.
  2. Get State:

    pn.GetState().Channels([]string).ChannelGroups([]string).UUID(string).QueryParam(queryParam).Execute()
    

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
Stateinterface{}map of UUIDs and the user states.

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

MethodTypeDescription
Statemap[string]interface{}map of UUIDs and the user states.

Other Examples

  1. 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:

  1. 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

  1. To start heartbeating without subscription to channel or channel group

    pn.Presence().
        Connected(true).
        Channels([]‌string{"my-channel"}).
        ChannelGroups([]string{"my-channel-group"}).
        Execute()
    
  2. To stop heartbeating without subscription to channel or channel group

    pn.Presence().
        Connected(false).
        Channels([]‌string{"my-channel"}).
        ChannelGroups([]string{"my-channel-group"}).
        Execute()
    
← Publish & SubscribeAccess Manager →
  • Here Now
    • Description
    • Method(s)
    • Basic Usage
    • Rest Response from Server
    • Other Examples
  • Where Now
    • Description
    • Method(s)
    • Basic Usage
    • Rest Response from Server
    • Other Examples
  • User State
    • Description
    • Method(s)
    • Basic Usage
    • Response
    • Other Examples
  • Heartbeat without subscription
    • Description
    • Method(s)
    • Basic Usage
    • Other Examples
© PubNub Inc. - Privacy Policy