SelectRuby Configuration API Reference for Realtime Apps

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

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


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

Ruby complete API reference for building Realtime Applications on PubNub, including basic usage and sample code.

require 'pubnub'
This function is used for initializing the PubNub Client API context. This function must be called before attempting to utilize any API functionality in order to establish account level credentials such as publish_key and subscribe_key.
To Initialize Pubnub you can use the following method(s) in the Ruby SDK:
  1. ParameterTypeRequiredDescription
    originStringOptionalSpecifies the fully qualified domain name of the PubNub origin. By default this value is set to but it should be set to the appropriate origin specified in the PubNub Admin Portal.
    publish_keyStringOptionalSpecifies the publish_key to be used for publishing messages to a channel. This key can be specified at initialization or along with apublish() .
    subscribe_keyStringYesSpecifies the subscribe_key to be used for subscribing to a channel. This key can be specified at initialization or along with a subscribe() .
    uuidStringOptionalSpecifies the unique user id to be used to identify the client. By default a randomly generated uuid is created by the client in the form of: 6dbee204-df0b-4a72-a478-2317e107ce02
    callbackFunctionOptionalSpecifies the name of the function that the callback executes on receiving data. If http_sync is also set to false then either callback or block_given must also be specified.
    connect_callbackFunctionOptionalCallback is called on successful connection to the PubNub Cloud.
    cipher_keyStringOptionalSpecifies a cryptographic key to use for message level encryption with AES. The cipher_key specifies the particular transformation of plain text into cipher text, or vice versa during decryption.
    error_callbackFunctionOptionalCallback is called on an error condition.
    http_syncBooleanOptionalIf set to true the call will execute in a synchronous fashion. If http_sync is not specified the default is false, the call will execute asynchronously, proceeding to the next instruction without waiting for a return to the previous instruction. When async is used either callback or block_given must also be specified.
    sslBooleanOptionalSetting a value of true enables transport layer encryption with SSL/TLS. Default is false.
    heartbeat IntegerOptionalSetting value of heartbeat enables heartbeat with given value. Heartbeat starts with first async subscribe. Disabled by default.
    subscribe_timeout IntegerOptionalChange default value of subscribe_timeout. Only advanced usage, can break client.
    non_subscribe_timeout IntegerOptionalChange default value of non_subscribe_timeout. Only advanced usage.
    max_retries IntegerOptionalNumber of retries when connection is broken before giving up.
    ttl IntegerOptionalDefault ttl value for grant and revoke events.
pubnub = 'my_subkey', publish_key: 'my_pubkey', ssl: true)
It returns the Pubnub instance for invoking PubNub APIs like publish(), subscribe(), history(), here_now(), etc.
  1. pubnub =
      publish_key: 'demo',
      subscribe_key: 'demo'
  2. In the case where a client will only read messages and never publish to a channel, you can simply omit the publish_key when initializing the client:
    # Initialize for Read Only Client
    pubnub =
      subscribe_key : 'demo'
  3. Set a custom UUID to identify your users.
     # initialize
    pubnub =
      publish_key: 'demo',
      subscribe_key: 'demo',
      uuid: 'Stephen'
  4. This examples demonstrates how to enable PubNub Transport Layer Encryption with SSL. Just initialize the client with ssl set to true. The hard work is done, now the PubNub API takes care of the rest. Just subscribe and publish as usual and you are good to go.

    pubnub = 'my_subkey', publish_key: 'my_pubkey', ssl: true)
  5. Requires Access Manager add-on XRequires that the Access Manager add-on is enabled for your key. See this page on enabling add-on features on your keys:

    Anyone with the secret_key can grant and revoke permissions to your app. Never let your secret key be discovered, and to only exchange it / deliver it securely. Only use the secret_key on secure server-side platforms.

    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.

    For applications that will administer PAM permissions, the API is initialized with the secret_key as in the following example:
    pubnub = 'my_subkey', secret_key: 'my_secretkey')
    Now that the pubnub object is instantiated the client will be able to access the PAM functions. The pubnub object will use the secret_key to sign all PAM messages to the PubNub Network.
This function is used to set a user ID on the fly.
To set UUID you can use the following method(s) in Ruby SDK:
  1. ParameterTypeRequiredDescription
    callbackFunctionOptionalWhen specified this callback returns UUID as a parameter.
# get random UUID
uuid =
  1. unique_channel =
      channel: unique_channel
    ) { |e| puts e.msg }
  2. uuid =
    pubnub =
      publish_key: 'demo',
      subscribe_key: 'demo',
      uuid: uuid
  3. auth_key =
Setter and getter for users auth key.
  1. ParameterTypeRequiredDescription
    auth_keyStringRequiredSpecifies auth_key to use to determine User-Level Access Manager permissions.
  2. This method doesn't take any arguments.
 pubnub.set_auth_key('my_new_authkey' )
pubnub.auth_key = nil
Get Auth key returns the current authentication key.

Go to Publish & Subscribe