RubyRuby V4 Configuration API Reference for Realtime Apps

 

These docs are for PubNub 4.0 for Ruby which is our latest and greatest! For the docs of the older versions of the SDK, please check PubNub 3.0 for Ruby.

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

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

In order to use pubnub ruby gem you need to install it. You can do it via rubygems command:
	gem install pubnub
Or add it to your Gemfile.
	gem 'pubnub', '~> 4.1.6'
To use pubnub you need to require it in your files.
	require 'pubnub'
That's it, you can use pubnub ruby client right away.
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 V4 SDK:
  1. ParameterTypeRequiredDefaultsDescription
    subscribe_keyString, SymbolYesYour subscribe key.
    publish_keyString, SymbolOptionalYour publish key, required to publish messages.
    secret_keyString, SymbolOptionalYour secret key, required for PAM.
    auth_keyString, SymbolOptionalYour auth key.
    cipher_keyStringOptionalYour cipher key, it's used to encrypt and decrypt messagages if set.
    sslBooleanOptionalfalseIf set to true ssl connection will be used.
    uuidStringOptionalrandom generated uuidIf not set, it will be generated by pubnub client. Has to be unique.
    heartbeatIntegerOptionalSetting value of heartbeat enables heartbeat with given value. Heartbeat starts with first async subscribe. Disabled by default.
    callbackLambdaOptionalThat callback will be automatically passed to all method calls fired from this client (like publish, history, subscribe). Will be overwritten by the callback passed to called event.
    ttlIntegerOptionalDefault ttl for grant and revoke.
    open_timeoutIntegerOptional10

    Timeout for opening connection for non-subscribe events.


    The value is in seconds.

    read_timeoutIntegerOptional10

    Timeout for reading for non-subscribe events.


    The value is in seconds.

    idle_timeoutIntegerOptional10

    Timeout for idle for non-subscribe events.


    The value is in seconds.

    s_open_timeoutIntegerOptional310

    Timeout for opening connection for subscribe.


    The value is in seconds.

    s_read_timeoutIntegerOptional310

    Timeout for read for subscribe.


    The value is in seconds.

    s_idle_timeoutIntegerOptional310

    Timeout for idle for subscribe.


    The value is in seconds.

    loggerObjectOptionalLogger instance that outputs logs to 'pubnub.log'Custom logger instance.
    originStringOptionalps.pndsn.com

    Custom origin.

    Add method that allows changing the origin.


    #origin=(origin)

    http_syncBooleanOptionalfalse

    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.

pubnub = Pubnub.new(
	subscribe_key: :demo,
	publish_key:   :demo,
	ssl: true
)
It returns the Pubnub instance for invoking PubNub APIs like publish(), subscribe(), history(), here_now(), etc.
  1. pubnub = Pubnub.new(
    	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 = Pubnub.new(
    	subscribe_key : 'demo'
    )
  3. Set a custom UUID to identify your users.
    # initialize
    pubnub = Pubnub.new(
    	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 = Pubnub.new(
    	subscribe_key: :demo,
    	publish_key:   :demo,
    	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:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-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 = Pubnub.new(subscribe_key: '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 provides the capability to get a user's auth_key.
  1. This method doesn't take any arguments.
pubnub.auth_key
The current authentication key.

Go to Publish & Subscribe