Ruby 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 here_now()
function in your application.
Method(s)
To call Here Now
you can use the following method(s) in the Ruby V4 SDK:
here_now(channels: channels, channel_groups: channel_groups, http_sync: http_sync, callback: callback)
Parameter | Type | Required | Description |
---|---|---|---|
channels | String, Symbol | Optional | Specify the channels name to return occupancy results. If channels is not provided, here_now will return data for all channels (global here_now ). |
channel_groups | String, Symbol | Optional | Specify the channel_groups name to return occupancy results. |
http_sync | Boolean | Optional | 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. |
callback | Lambda accepting one parameter | Optional | 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). |
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
}
>
Other Examples
- 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
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" }
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 where_now()
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 where_now()
you can use the following method(s) in the Ruby V4 SDK:
where_now(uuid: uuid, http_sync: http_sync, callback: callback)
Parameter | Type | Required | Description |
---|---|---|---|
uuid | String | Yes | UUID we are looking for. |
http_sync | Boolean | Optional | 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. |
callback | Lambda accepting one parameter | Optional | 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). |
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-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)
set_state(channels: channels, state: state, http_sync: http_sync, callback: callback)
Parameter | Type | Required | Description |
---|---|---|---|
channels | String, Symbol | Yes | Specify channels name to set state for. |
state | Hash | Yes | The state to set. |
http_sync | Boolean | Optional | 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. |
callback | Lambda accepting one parameter | Optional | 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). |
get_state(channels: channels, uuid: uuid, http_sync: http_sync, callback: callback)
Parameter | Type | Required | Description |
---|---|---|---|
channels | String, Symbol | Yes | Specify channels name to get state from. |
uuid | String | Optional | Specifies UUID . |
http_sync | Boolean | Optional | 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. |
callback | Lambda accepting one parameter | Optional | 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). |
Basic Usage
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
Response
#<Pubnub::Envelope:0x007fd3851b5cc8
@result = {
:data => {
:state => {
"age"=>59,
"first" => "Robert",
"last" => "Plant"
},
:channel=>"whatever"
}
},
@status = {
:code => 200
}
>