RubyRuby V4 Presence API Reference for Realtime Apps

 

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

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

Presence enables you to track the online and offline status of users and devices in realtime, 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.


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 here_now() function in your application.
To call Here Now you can use the following method(s) in the Ruby V4 SDK:
  1. ParameterTypeRequiredDescription
    channelsString, SymbolOptionalSpecify the channels name to return occupancy results. If channels is not provided, here_now will return data for all channels (global here_now).
    channel_groupsString, SymbolOptionalSpecify the channel_groups name to return occupancy results.
    http_syncBooleanOptional

    Default false. Method will be executed asynchronously and will return future, to get it's value you can use value method. If set to true, method will return array of envelopes (even if there's only one envelope).

    For sync methods Envelope object will be returned.

    callbackLambda accepting one parameterOptional

    Callback that will be called for each envelope.

    For async methods future will be returned, to retrieve value Envelope object you have to call value method (thread will be locked until the value is returned).

pubnub.here_now(
	channel: 'my_channel',
) do |envelope| 
	puts envelope
end
#<Pubnub::Envelope
    @result = {
        :data => {
            :uuids => ["2d588b75-0451-4bde-8952-13128c10e952"],
            :occupancy => 1
        }
    },
    @status = {
        :code => 200
    }
>
  1. 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
    pubnub.here_now() do |envelope| 
    	puts envelope
    end
    {
        "status": 200,
        "message": "OK",
        "payload": {
            "channels": {
                "81d8d989-b95f-443c-a726-04fac323b331": { 
                    "uuids": [ "70fc1140-22b5-4abc-85b2-ff8c17b24d59" ], 
                    "occupancy": 1 
                },
                "81b7a746-d153-49e2-ab70-3037a75cec7e": { 
                    "uuids": [ "91363e7f-584b-49cc-822c-52c2c01e5928" ],
                    "occupancy": 1
                },
                "c068d15e-772a-4bcd-aa27-f920b7a4eaa8": { 
                    "uuids": [ "ccfac1dd-b3a5-4afd-9fd9-db11aeb65395" ],
                    "occupancy": 1
                }
            },
            "total_channels": 3,
            "total_occupancy": 3
        },
        "service": "Presence"
    }

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 where_now() 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 where_now() you can use the following method(s) in the Ruby V4 SDK:
  1. ParameterTypeRequiredDescription
    uuidStringYesUUID we are looking for.
    http_syncBooleanOptional

    Default false. Method will be executed asynchronously and will return future, to get it's value you can use value method. If set to true, method will return array of envelopes (even if there's only one envelope).

    For sync methods Envelope object will be returned.

    callbackLambda accepting one parameterOptional

    Callback that will be called for each envelope.

    For async methods future will be returned, to retrieve value Envelope object you have to call value method (thread will be locked until the value is returned).

You simply need to define the uuid and the callback function to be used to send the data to as in the example below.
pubnub.where_now(
	uuid: "my_uuid"
) do |envelope|
	puts envelope.result[:data]
end
#<Pubnub::Envelope:0x007fd38584c168
	@result = {
		:data => {
		"channels" => ["whatever"]
		}
	},
	@status = {
		:code =>200
	}
>

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
    channelsString, SymbolYesSpecify channels name to set state for.
    stateHashYesThe state to set.
    http_syncBooleanOptional

    Default false. Method will be executed asynchronously and will return future, to get it's value you can use value method. If set to true, method will return array of envelopes (even if there's only one envelope).

    For sync methods Envelope object will be returned.

    callbackLambda accepting one parameterOptional

    Callback that will be called for each envelope.

    For async methods future will be returned, to retrieve value Envelope object you have to call value method (thread will be locked until the value is returned).

  1. ParameterTypeRequiredDescription
    channelsString, SymbolYesSpecify channels name to get state from.
    uuidStringOptionalSpecifies UUID.
    http_syncBooleanOptional

    Default false. Method will be executed asynchronously and will return future, to get it's value you can use value method. If set to true, method will return array of envelopes (even if there's only one envelope).

    For sync methods Envelope object will be returned.

    callbackLambda accepting one parameterOptional

    Callback that will be called for each envelope.

    For async methods future will be returned, to retrieve value Envelope object you have to call value method (thread will be locked until the value is returned).

pubnub.set_state(channel: 'my_channel', state: { key: 'value' }) do |envelope|
	puts envelope.status
end
pubnub.state(channel: :my_channel, uuid: 'some-uuid') do |envelope|
	puts envelope.result[:data][:state]
end
#<Pubnub::Envelope:0x007fd3851b5cc8
    @result = {
        :data => {
            :state => {
                "age"=>59,
                "first" => "Robert",
                "last" => "Plant"
            }, 
            :channel=>"whatever"
        }
    },
    @status = {
        :code => 200
    }
>