PubNub Ruby SDK 5.3.5
This page outlines the steps to follow to create a simple Hello, World application with PubNub. This covers the basics of integrating PubNub in your application: setting up a connection to PubNub, and sending and receiving messages.
PubNub account
Sign in or create an account to create an app on the Admin Portal and get the keys to use in your application.
When you create a new app, the first set of keys is generated automatically, but a single app can have as many keysets as you like. We recommend that you create separate keysets for production and test environments.
Download the SDK
Download the SDK from any of the following sources:
Use RubyGems
Integrate PubNub into your project using RubyGems.
gem install pubnub
You can also add pubnub
to your Gemfile:
gem 'pubnub', '~> 5.3.5'
Get the source code
https://github.com/pubnub/ruby
View the supported platforms here.
Configure PubNub
In the IDE of your choice, create a new App.rb
file and add the following code. This is the minimum configuration you need to send and receive messages with PubNub.
Make sure to replace myPublishKey and mySubscribeKey with your app's publish and subscribe keys from the Admin Portal.
require 'pubnub'
pubnub = Pubnub.new(
subscribe_key: :mySubscribeKey,
publish_key: :myPublishKey,
user_id: :myUniquesUserId
)
For more information, refer to the Configuration section of the SDK documentation.
Add event listeners
Listeners help your app react to events and messages. You can implement custom app logic to respond to each type of message or event.
Copy the code below to configure your app such that when it receives a message, the content of the message is printed.
callback = Pubnub::SubscribeCallback.new(
message: ->(envelope) {
puts "MESSAGE: #{puts envelope.result[:data][:message]['msg']}"
},
presence: ->(envelope) {
puts "PRESENCE: #{envelope.result[:data]}"
}
)
pubnub.add_listener(callback: callback)
For more information, refer to the Listeners section of the SDK documentation.
Publish and subscribe
To receive messages sent to a particular channel, you subscribe to it. When you publish a message to a channel, PubNub delivers that message to everyone subscribed to that channel.
In this app, publishing a message is triggered when you send the publish
call. To subscribe, you send a subscribe
call.
pubnub.subscribe(
channels: ['my_channel'],
with_presence: true
)
pubnub.publish(
channel: 'my_channel',
message: { text: 'Hello world' }
) do |envelope|
puts envelope.status
end
For more information, refer to the Publish and Subscribe section of the SDK documentation, and to Message Publish.
Putting it all together
Your App.rb
file should now look similar to the following:
require 'pubnub'
pubnub = Pubnub.new(
subscribe_key: :mySubscribeKey,
publish_key: :myPublishKey,
user_id: :myUniqueUserId
)
callback = Pubnub::SubscribeCallback.new(
message: ->(envelope) {
puts "MESSAGE: #{puts envelope.result[:data][:message]['msg']}"
},
presence: ->(envelope) {
puts "PRESENCE: #{envelope.result[:data]}"
}
)
show all 28 linesNow, run your app to see if you did everything correctly. You should see "Hello world" printed in the console.
Congratulations! You've just subscribed to a channel and sent your first message.
Walkthrough
Instead of focusing on the order in which you wrote the code, let's focus on the order in which it runs. The app you just created does a few things:
- configures a PubNub connection
- adds the
message
, andpresence
event listeners - subscribes to a channel
- publishes a message
Configuring PubNub
The following code is the minimum configuration you need to send and receive messages with PubNub. For more information, refer to the Configuration section of the SDK documentation.
require 'pubnub'
pubnub = Pubnub.new(
subscribe_key: :mySubscribeKey,
publish_key: :myPublishKey,
user_id: :myUniqueUserId
)
Add event listeners
Listeners help your app react to events and messages. You can implement custom app logic to respond to each type of message or event.
You added two listeners to the app: presence
and message
. Presence prints out the presence events that are sent to the channel. The message listener listens for incoming messages on a particular channel. When it receives a message, the app simply prints the received message. This is why you see "Hello world" displayed in the console.
callback = Pubnub::SubscribeCallback.new(
message: ->(envelope) {
puts "MESSAGE: #{puts envelope.result[:data][:message]['msg']}"
},
presence: ->(envelope) {
puts "PRESENCE: #{envelope.result[:data]}"
}
)
pubnub.add_listener(callback: callback)
For more information, refer to the Listeners section of the SDK documentation.
Publishing and subscribing
PubNub uses the Publish/Subscribe model for real-time communication. This model involves two essential parts:
- Channels are transient paths over which your data is transmitted
- Messages contain the data you want to transmit to one or more recipients
When you want to receive messages sent to a particular channel, you subscribe to it. When you publish a message to a channel, PubNub delivers that message to everyone who is subscribed to that channel. In this example, you subscribe to a channel named my_channel
.
A message can be any type of JSON-serializable data (such as objects, arrays, integers, strings) that is smaller than 32 KiB. PubNub will, in most cases, deliver your message to its intended recipients in fewer than 100 ms regardless of their location. You can also share files up to 5MB.
You can subscribe to more than one channel with a single subscribe call but in this example, you subscribe to a single channel:
pubnub.subscribe(
channels: ['my_channel'],
with_presence: true
)
The publish()
method sends the "Hello world" message:
pubnub.publish(
channel: 'my_channel',
message: { text: 'Hello world!' }
) do |envelope|
puts envelope.status
end
For more information, refer to the Publish and Subscribe section of the SDK documentation, and to Message Publish.
Next steps
You have just learned how to use the Ruby SDK to send and receive messages using PubNub. Next, take a look at the SDK's reference documentation which covers PubNub API in more detail.