SelectRuby SDK 3.8.5

These docs are for version 3.8.5 of the Ruby SDK. To check out the docs for the latest version Click Here.
 

About Ruby 2.0.0 - from the Ruby Community https://www.ruby-lang.org/en/news/2016/02/24/support-plan-of-ruby-2-0-0-and-2-1/

As it has been announced before, all support for Ruby 2.0.0 has ended today. Bug and security fixes from more recent Ruby versions will no longer be back-ported to 2.0.0, and no further patch release of 2.0.0 will be released.

We highly recommend that you upgrade to Ruby 2.3 or 2.2 and to the latest PubNub Ruby SDK http://www.pubnub.com/docs/ruby/pubnub-ruby-sdk

 

PubNub 4.0 for Ruby is our latest and greatest! Please click here for our PubNub 4.0 for Ruby docs and SDK.

PubNub 3.x for Ruby will encounter End Of Life (EOL) April 1st, 2017. All users currently on 3.x should begin migrating to 4.x. The 3.x SDK will continue to work. We will no longer support it and if you run into an issue, we will request that you upgrade to the latest version before solving the issue.

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


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
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
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.

When you init with secret_key, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

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 realtime 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