GoGo V4 Presence API Reference for Realtime Apps

Go to Publish & Subscribe


 

These docs are for PubNub 4.0 for Go which is our latest and greatest! For the docs of the older versions of the SDK, please check PubNub 3.0 for Go.

If you have questions about the PubNub for Go SDK, please contact us at support@pubnub.com.

Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-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.
To call Here Now you can use the following method(s) in the Go V4 SDK:
  1. 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.
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)
The HereNow() operation returns a. PNHereNowResult which contains the following fields:
MethodTypeDescription
TotalChannelsintTotal Channels
TotalOccupancyintTotal Occupancy
Channels[]HereNowChannelData
MethodTypeDescription
ChannelNamestringChannel name
OccupancyintOccupancy of the Channel
Occupants[]HereNowOccupantsData
MethodTypeDescription
UUIDstringUUID of the user
Statemap[string]interface{}State of the user.
  1. Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/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
        }
    }
  2. Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/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()
    for _, v := range res.Channels {
        fmt.Println(v.ChannelName) // my_channel
        fmt.Println(v.Occupancy) // 3
    }
  3. Requires Presence add-on XRequires 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:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    You can return the list of UUIDs and occupancy for all channels 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;
        }
    }
  4. Requires Presence add-on XRequires 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:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    You can return only the occupancy information (Global Here Now) by omitting the channel 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;
        }
    }
  5. 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
Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-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.
 
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.
To call WhereNow() you can use the following method(s) in the Go V4 SDK:
  1. 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.
You simply need to define the UUID to be used to send the data to as in the example below.
res, status, err := pn.WhereNow().
    UUID("username-uuid"). // uuid of the user we want to spy on
    Execute()
MethodTypeDescription
Channels[]stringThe list of channels where the UUID is present.
  1. 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()
Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-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.
  1. 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.
  1. 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.
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)
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.
  1. 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)
    }
    

Go to Access Manager