PubNub Ruby SDK 3.7.8

gem install pubnub


require 'pubnub'
pubnub = Pubnub.new(
  subscribe_key: 'sub-key', 
  publish_key: 'pub-key', 
  connect_callback: lambda {|msg| pubnub.publish(channel: 'my_channel', message: 'Hello from PubNub Ruby SDK!!', http_sync: true)}
)
 
pubnub.subscribe(channel: 'my_channel') do |envelope|
  puts envelope.message
end

Instantiate a new Pubnub instance. Only the subscribe_key is mandatory. Also include publish_key if you intend to publish from this instance, and the secret_key if you wish to perform PAM administrative operations from this Ruby instance.

 

For security reasons you should only include the secret-key on a highly secured server. The secret-key is only required for granting rights using our Access Manager.

pubnub = Pubnub.new(
  publish_key: 'demo',
  subscribe_key: 'demo'
)

Call time() to verify the client connectivity to the origin:

pubnub.time do |envelope|
  puts envelope.message
end

Subscribe (listen on) a channel (it's async!):

pubnub.subscribe(
  channel: 'my_channel'
) do |envelope| 
  puts envelope.message
end

Publish a message to a channel:

pubnub.publish(
  channel: 'my_channel',
  message: 'Hello from PubNub Ruby SDK!'
) do |envelope| 
  puts envelope.parsed_response
end
 

Get occupancy of who's here now on the channel by UUID:

 

Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys

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

Subscribe to realt-time Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:

 

Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys

pubnub.presence(
  channel: 'my_channel'
) do |envelope|
  puts envelope.message
end

Retrieve published messages from archival storage:

 

Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys

pubnub.history(
    channel: 'history_channel',
    count: 100
) do |envelope|
  puts envelope.message
end

Stop subscribing (listening) to a channel.

pubnub.unsubscribe(
  channel: 'my_channel'
) do |envelope|
  puts envelope.message
end