PubNub

This add-on is operated by PubNub

Data Push for Any Device (Mobile, Tablet, Browser, Desktop, Server)

PubNub

Last Updated: 19 March 2014

api

Table of Contents

Using Publish Subscribe could not be simpler.

PubNub Ruby on Heroku

PubNub provides a web-service API for businesses to build scalable Data Push communication apps on Mobile, Tablet and Web. Use PubNub to send/receive messages in real-time across multiple platforms and devices with two simple API calls (Publish/Subscribe - Send/Receive).

Get PubNub gem

Ruby Gems link to registered Ruby Gem.

> gem install pubnub

Use PubNub on Heroku

require 'pubnub'

publish_key   = ENV['PUBNUB_PUBLISH_KEY'] || 'demo'
subscribe_key = ENV['PUBNUB_SUBSCRIBE_KEY'] || 'demo'

## -----------------------------------------
## Create Pubnub Client API (INITIALIZATION)
## -----------------------------------------
puts('Creating new PubNub Client API')

pubnub = Pubnub.new(
     :subscribe_key    => 'demo',
     :publish_key      => 'demo',
     :error_callback   => lambda { |msg|
        puts "ERROR: #{msg.inspect}"
     },
     :connect_callback => lambda { |msg|
        puts "CONNECTED: #{msg.inspect}"
     }
)

## ----------------------
## Send Message (PUBLISH)
## ----------------------
puts('Broadcasting Message')
message = { 'some_data' => 'my data here' }

info = pubnub.publish(
    :channel => "hello_world",
    :message => message
) { |envelope|
   puts("\nchannel: #{envelope.channel}
        \nmsg: #{envelope.message}")
}

## Publish Success?
puts(info)

## --------------------------------
## Request Past Publishes (HISTORY)
## --------------------------------
puts('Requesting History')
messages = pubnub.history(
    :channel => 'hello_world',
    :count   => 100
){ |envelope|
   puts("\nchannel: #{envelope.channel}:
   \nmsg: #{envelope.message}")
}

puts(messages)

Use PubNub to send and receive messages

PubNub Ruby SDK Documentation