Ruby Data Stream Tutorial for Real-Time Apps | PubNub

You when you are finished with this tutorial you will know how to say "Hello!" to the PubNub Real-Time Network with the Ruby SDK. We’ll cover the following:

To get a client to send and receive data from a channel requires only a few simple-to-use APIs:

For most basic Data Stream applications, this is all you need to start building real-time applications with PubNub.

If you would like to first review PubNub Data Stream features go here.

Tutorial Contents:

Getting Started

To complete the exercises in this tutorial you’ll need to have installed Ruby IRB ( or Pry ).

Step 1: Install The Code

On Ruby the first step is to install the PubNub gem by using the command line below:

Install the PubNub gem
 gem install pubnub

Next we’ll open up IRB and include the PubNub Ruby SDK in our application:

Require the SDK
 require 'pubnub'

If you get this far, you are ready to rock.

  1. First install the gem

  2. Run IRB

  3. Copy the lines into the IRB and hit enter.

Step 2: Access The API

Now it is time to get down to business, but before we start sending and receiving messages, we need to set up the appropriate credentials to access the PubNub network. We do this by initializing our PubNub API. Here we can define the publish_key and subscribe_key we will need to publish and subscribe to a channel.

 pubnub =
     :publish_key   => 'demo',
     :subscribe_key => 'demo',
     :uuid => 'i_am_a_real_time_boss_1'

In the example code we created the Pubnub object and now we can access the available PubNub API methods. At this point you are probably eager to start sending messages. Read on real-time adventurer!

Step 3: Receive Messages

This is where the fun begins! If we want to receive messages from a channel we need to subscribe to a channel. All we need to do is provide a channel name.

Subscribe to a channel
    :channel  => 'a'
 ){ |data| puts data.msg }

Once you have subscribed you should see a very interesting message stream that prints a new message every second.

  1. Paste the example code above into the Ruby console and hit enter.

  2. Enjoy the message stream.

Step 4: Stop Receiving Messages

Ok, this non-stop stream of messages is now starting to get a bit old. How do we make it stop? We simply unsubscribe from the channel.

Leaving a channel
    :channel  => 'a'
 ){ |data| puts data.msg }

You’ll notice that you receive a message that says:

  1. Paste the example code above into the Ruby console and hit enter.

  2. Enjoy the silence.

Step 5: Send Messages

Now its time to find our voice. Its time to send a message to everyone around the world subscribed to channel demo. First we’ll subscribe as we did in Step 3 only this time we’ll specify channel as demo.

Once we are subscribed to the channel all we need to do is use the Publish API, specify the channel name and message we’d like to send. We’ll also include code to capture the response from PubNub.

Subscribe & Publish to channel demo
    :channel  => 'demo'
 ) { |data| puts data.msg }
     :channel  => 'demo',
     :message => 'Hello PubNub, love the Ruby SDK!',
 ) { |data| puts data.response }

That’s it, as soon as you make the publish call your message is replicated around the world and received by anyone subscribed to the channel.

You should see that once you publish the message a response is sent from PubNub that looks similar to the example below:


The status message confirms your message was published to the PubNub cloud and the timetoken indicates at what time the message was sent (as a 17-digit precision unix epoch). These timetokens are used throughout PubNub to keep track of when messages are sent. In the Storage & Playback tutorial, you’ll learn that it is possible to retrieve a particular message or a whole range of messages based on the associated timetokens.

You’ll see that once you’ve published this message you will get the same message sent back to you from the PubNub Network. That’s all there is to it. You can try sending as many messages as you like.

Step 6: Send Messages To and Fro

To extend the example a bit further we can simulate passing messages back and forth between two different users.

The PubNub Developer Console makes interacting with your application across the PubNub Real-Time Network very easy. You can simulate a remote client and send and receive messages from the console.

Once in the console you can enter a message into the message box on the left. After you send the message you should see it received on your Ruby app’s console.

Additional Resources

If you are interested in a more advanced (fancy!) example checkout this video:

Advanced Tutorial

For additional Data Stream resources take a look at:

  • The full PubNub API reference for Ruby SDK: here

  • Data Stream overview: here

  • PubNub REST API here