Presence API for PubNub Ruby 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 here_now() 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 Ruby SDK:

here_now(
channels: channels,
channel_groups: channel_groups,
http_sync: http_sync,
callback: callback
)
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_syncBooleanOptionalDefault 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 parameterOptionalCallback 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).

Basic Usage

Get a list of uuids subscribed to channel

pubnub.here_now(
channel: 'my_channel',
) do |envelope|
puts envelope
end

Response

#<Pubnub::Envelope
@result = {
:data => {
:uuids => ["2d588b75-0451-4bde-8952-13128c10e952"],
:occupancy => 1
}
},
@status = {
:code => 200
}
>

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 where_now() 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 where_now() you can use the following method(s) in the Ruby SDK:

where_now(
uuid: uuid,
http_sync: http_sync,
callback: callback
)
ParameterTypeRequiredDescription
uuidStringYesUUID we are looking for.
http_syncBooleanOptionalDefault 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 parameterOptionalCallback 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).

Basic Usage

You simply need to define the uuid and the callback function to be used to send the data to as in the example below.

Get a list of channels a UUID is subscribed to

pubnub.where_now(
uuid: "my_uuid"
) do |envelope|
puts envelope.result[:data]
end

Response

#<Pubnub::Envelope:0x007fd38584c168
@result = {
:data => {
"channels" => ["whatever"]
}
},
@status = {
:code =>200
}
>

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(
channels: channels,
state: state,
http_sync: http_sync,
callback: callback
)
ParameterTypeRequiredDescription
channelsString, SymbolYesSpecify channels name to set state for.
stateHashYesThe state to set.
http_syncBooleanOptionalDefault 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 parameterOptionalCallback 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).
get_state(
channels: channels,
uuid: uuid,
http_sync: http_sync,
callback: callback
)
ParameterTypeRequiredDescription
channelsString, SymbolYesSpecify channels name to get state from.
uuidStringOptionalSpecifies UUID.
http_syncBooleanOptionalDefault 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 parameterOptionalCallback 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).

Basic Usage

Set State

pubnub.set_state(channel: 'my_channel', state: { key: 'value' }) do |envelope|
puts envelope.status
end

Get State

pubnub.state(channel: :my_channel, uuid: 'some-uuid') do |envelope|
puts envelope.result[:data][:state]
end

Response

#<Pubnub::Envelope:0x007fd3851b5cc8
@result = {
:data => {
:state => {
"age"=>59,
"first" => "Robert",
"last" => "Plant"
},
:channel=>"whatever"
}
},
@status = {
:code => 200
}
>
Last updated on