Presence API for Ruby SDK
Presence lets you track who is online or offline and store custom state information. Presence shows:
- When a user has joined or left a channel
- How many users are subscribed to a particular channel (occupancy)
- Which channels a user or device is subscribed to
- Presence state associated with these users
Learn more about our Presence feature in the Presence overview.
Here now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
This method returns information about the current state of a channel, including a list of unique user IDs (universally unique identifiers, UUIDs) currently subscribed to the channel and the total occupancy count of the channel.
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
)
Parameter | Description |
---|---|
channels Type: String, Symbol | 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 Type: String, Symbol | Specify the channel_groups name to return occupancy results. Wildcards are not supported. |
http_sync Type: Boolean | 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 Type: Lambda accepting one parameter | 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). |
Sample code
Get a list of uuids subscribed to channel
Reference code
require 'pubnub'
def fetch_uuids(pubnub)
pubnub.here_now(
channel: 'my_channel'
) do |envelope|
if envelope.status[:error]
puts "Error fetching UUIDs: #{envelope.status[:error]}"
else
puts "UUIDs subscribed to my_channel: #{envelope.result[:data][:uuids]}"
puts "Occupancy: #{envelope.result[:data][:occupancy]}"
end
end
end
show all 30 linesResponse
#<Pubnub::Envelope
@result = {
:data => {
:uuids => ["2d588b75-0451-4bde-8952-13128c10e952"],
:occupancy => 1
}
},
@status = {
:code => 200
}
>
Where now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
This method returns the list of channels a UUID is subscribed to.
Timeout events
If the app restarts (or the page refreshes) 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
)
Parameter | Description |
---|---|
uuid *Type: String | UUID we are looking for. |
http_sync Type: Boolean | 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 Type: Lambda accepting one parameter | 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). |
Sample code
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
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
Clients can set a dynamic custom state (score, game state, location) for their users on one or more channels and store it on a channel as long as the user stays subscribed.
The state is not persisted, and when the client disconnects, the state data is lost. For more information, refer to Presence State.
Method(s)
set_state(
channels: channels,
state: state,
http_sync: http_sync,
callback: callback
)
Parameter | Description |
---|---|
channels *Type: String, Symbol | Specify channels name to set state for. |
state *Type: Hash | The state to set. |
http_sync Type: Boolean | 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 Type: Lambda accepting one parameter | 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 | Description |
---|---|
channels *Type: String, Symbol | Specify channels name to get state from. |
uuid Type: String | Specifies UUID . |
http_sync Type: Boolean | 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 Type: Lambda accepting one parameter | 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). |
Sample code
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
}
>