Give Feedback for the page

RubyPubNub Ruby V4 SDK 4.0.15

 

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.

gem install pubnub

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.0.15'
To use pubnub you need to require it in your files.
	require 'pubnub'
That's it, you can use pubnub ruby client right away.
pubnub = Pubnub.new(
	subscribe_key: :demo,
	publish_key: :demo
)

callback = Pubnub::SubscribeCallback.new(
	message:  ->(envelope) { 
		puts "MESSAGE: #{envelope.result[:data]}" 
	},
	presence: ->(envelope) {
		puts "PRESENCE: #{envelope.result[:data]}" 
	},
	status:   lambda do |envelope|
		puts "\n\n\n#{envelope.status}\n\n\n"

		if envelope.error?
			puts "ERROR! #{envelope.status[:category]}"
		else
			puts 'Connected!' if envelope.status[:last_timetoken] == 0 # Connected!
			pubnub.publish(
				channel: :demo, 
				message: 'Connected!'
			)
		end
    end
)

pubnub.add_listener(callback: callback)

pubnub.subscribe(
	channels: :demo,
	presence: :demo
)
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 V4 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'
)
callback = Pubnub::SubscribeCallback.new(
	message:  ->(_envelope) {}, # this will be fired only for non-presence messages
	presence: ->(_envelope) {}, # this will be fired only for presence messages
	status:   ->(envelope)  do  # this will be fired for status messages and errors
	if envelope.status[:error]
		case envelope.status[:category]
			when Pubnub::Constants::STATUS_ACCESS_DENIED # :access_denied
				# Access denied. Double check PAM etc.
			when Pubnub::Constants::STATUS_TIMEOUT # :timeout
				# Timeout error
			when Pubnub::Constants::STATUS_NON_JSON_RESPONSE # :non_json_response
				# Non json response
			when Pubnub::Constants::STATUS_API_KEY_ERROR # :api_key_error
				# API key error
			when Pubnub::Constants::STATUS_ERROR
				# Other error
			else
				# Shouldn't happen
			end
		end
	end
)

pubnub.add_listener(name: 'my_listener', callback: callback)
pubnub.remove_listener(name: 'my_listener')
# or
pubnub.remove_listener(callbacks)
# Init pubnub client
pubnub_client = Pubnub.new(subscribe_key: 'demo', publish_key: 'demo')

# First callbacks object
callbacks0 = Pubnub::SubscribeCallback.new(
  message:  ->(envelope) { puts "C0 MESSAGE: #{envelope.result[:data][:message]}" },
  presence: ->(envelope) { puts "C0 PRESENCE: #{envelope.result[:data][:message]}" },
  status:   ->(envelope) { puts "C0 STATUS: #{envelope.result[:data][:message]}" }
)

# Second callbacks object
callbacks1 = Pubnub::SubscribeCallback.new(
  message:  ->(envelope) { puts "C1 MESSAGE: #{envelope.result[:data][:message]}" },
  presence: ->(envelope) { puts "C1 PRESENCE: #{envelope.result[:data][:message]}" },
  status:   ->(envelope) { puts "C1 STATUS: #{envelope.result[:data][:message]}" }
)

# Adding listener allows you to specify name, it's not required to specify a name
pubnub_client.add_listener(name: 'c0', callback: callbacks0)

# Let's subscribe somewhere
pubnub_client.subscribe(channel: :demo, presence: :demo)

# SOME OUTPUT:
# C0 PRESENCE: {"action"=>"join", "timestamp"=>1461683357, "uuid"=>"fc0c0460-44b4-4338-b7e9-1b534b85072e", "occupancy"=>2}
# C0 MESSAGE: {"text"=>"hey"}
# C0 PRESENCE: {"action"=>"join", "timestamp"=>1461683374, "uuid"=>"3efb92f6-bf02-4373-aafa-996527718ecc", "occupancy"=>3}
# C0 MESSAGE: {"text"=>"hey"}

# Adding another subscriber
pubnub_client.add_listener(name: 'c1', callback: callbacks1)

# SOME OUTPUT WITH TWO LISTENERS ACTIVE:
# C0 MESSAGE: {"text"=>"hey"}
# C1 MESSAGE: {"text"=>"hey"}
# C0 PRESENCE: {"action"=>"leave", "timestamp"=>1461683435, "uuid"=>"3efb92f6-bf02-4373-aafa-996527718ecc", "occupancy"=>2}
# C1 PRESENCE: {"action"=>"leave", "timestamp"=>1461683435, "uuid"=>"3efb92f6-bf02-4373-aafa-996527718ecc", "occupancy"=>2}

# We're removing subscriber by giving it's name
pubnub_client.remove_listener(name: 'c1')

# SOME OUTPUT AFTER REMOVING C1 LISTENER
# C0 MESSAGE: {"text"=>"hey"}
# C0 MESSAGE: {"text"=>"hey"}
# C0 PRESENCE: {"action"=>"join", "timestamp"=>1461683698, "uuid"=>"3efb92f6-bf02-4373-aafa-996527718ecc", "occupancy"=>2}

# We're removing subsciber by giving it's object, now we don't have any listeners active
pubnub_client.remove_listener(callback: callbacks0)
Call time() to verify the client connectivity to the origin:
pubnub.time do |envelope|
    puts envelope.timetoken
end
# Subscribe to channel 'my_channel'.
pubnub.subscribe(
	channels: :my_channel
)
The response of the subscription is handled by Listener. Please see the Listeners section for more details.
Publish a message to a channel:
pubnub.publish(
    channel: 'my_channel',
    message: { text: 'Hi!' }
) do |envelope|
    puts envelope.status
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
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
# Subscribes to room0, room0-pnpres, room1, room1-pnpres, room2, room2-pnpres
pubnub.subscribe(
    channels: ['room0', 'room1', 'room2'], 
    with_presence: true
)
The response of the subscription is handled by Listener. Please see the Listeners section for more details.
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.result[:data][:messages]
end
pubnub.unsubscribe(
	channel: 'my_channel'
) do |envelope|
	puts envelope.status
end
The response of the subscription is handled by Listener. Please see the Listeners section for more details.