Implementation of PUBNUB in rails3

Create a new account in “http://www.pubnub.com/

Get the “User ID” and  API keys ( “Publish” , “subscribe”  and “secret” )

Download the Ruby-Push-Api from “https://github.com/pubnub/pubnub-api

Save the API plugins in “\vendor\plugins” folder

Add following gems to your Gemfile

gem ‘pubnub’

Then Run bundle

bundle install

Initilize the pubnub object in application controller

def mypubnub
Pubnub.new(:publish_key => ‘publish key’, # required
:subscribe_key => ‘subscribe key’, # required
:secret_key => ‘secet key’,
:ssl => false
)
end

Publishing the message through controller

def create
@my_callback = lambda { |message| puts(message[‘myname’]); return true}
begin
pb = mypubnub.publish({ ‘channel’ => ‘my_channel’,
‘message’ => ‘myname,myage,mysex’,
‘callback’ =>@my_callback
})
rescue
end
end

Subscribe the message in view file

#pubnub{:origin => “pubsub.pubnub.com”, “pub-key” => “publish_key”, :ssl => “off”, “sub-key” => “subscribe_key”}
%script{:src => “http://cdn.pubnub.com/pubnub-3.1.min.js”}

:javascript
PUBNUB.subscribe({
channel: ‘my_channel’,
limit: 1,
callback: function(message){
var ctr = parseInt($(“#realstorecount”).html());
ctr++;
$(“#realstorecount”).html(ctr);
$(“#listing_table”).prepend(message); // prepend your data
if(ctr == 1)
$(“#new_msg”).html(‘1 New task posted’);
else
$(“#new_msg”).html(ctr + ‘ New tasks posted’);

}
});

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s