venerdì 25 luglio 2014

How easily implement PubSub in your application with PubNub

I have recently got interested in PubSub message exchange pattern.
PubSub is a great way to decouple sender and receiver and to scale to large mass of subscribers.
For networking guys, this is somehow similar to multicast. I send my IP multicat packet and I don't know who is receiving it.
In PubSub is the same, Sender app send a message which is not addressed directly to any specific receiver. On the other side Receiver listen to the communication channel without looking for a specific client.

Here I am showing very basic usage of a great service called pubnub.
The service has a free tier for developer and for testing purposed. Should you find this service the right one for your application then you can purchase service directly on pubnub web site.

Anyway, let's see how to publish (send) a message and how to subscribe (receive) a message using pubnub.
I am showing code sample in Ruby, but please note that pubnub provide API/SDK to an impressive number of platform/languages

First of all register to pubnub. You can register with you Google account or create a free account.

After successful registration you will access to a page similar to this one




Here you need to take note of two important information:

  • Subscribe Key: necessary if you want to subscribe to channels and receive messages
  • Publish Key: necessary to send message to a channel

Let's now see the code of a very simple sender and receiver


# sudo gem install pubnub
# pubnub_receive.rb

#!/usr/bin/ruby

require 'pubnub'


pubnub = Pubnub.new(
 :publish_key   => 'your publish key',
    :subscribe_key => 'your subscribekey',
    :error_callback   => lambda { |msg| puts "Error connecting to PubNub: #{msg.inspect}" },
    :connect_callback => lambda { |msg| puts "Connected to PUBNUB: #{msg.inspect}" }
 )

message_cb = lambda { |msg|
 time = Time.now
 puts "-------Received Message-------"
 puts "Channel:: #{msg.channel}"
 puts "Message:: #{msg.message}"
 puts "Message received at #{time}"
 puts "------------------------------"
}

pubnub.subscribe(
    :channel  => 'mancusoa74demo',
    :callback => message_cb
 ) 

while(1) do
 puts "."
  sleep(5)
end

Pubnub.new creates a new pubnub object and connects us to the PubNub service.
You need to specify your publish and subscribe key otherwise you will not be able to send or receive messages.

Now that we have a valid pubnub object, we can subscribe to a channel.
This is done by calling the subscribe method on pubnub object.
We need to pass (at minimum) a channel name. Here I am also specifying a callback which is executed every time we receive a message from the channel.

In the call back I simply print out the channel name, the received message and the time on which the message has been received.

As we want to listen to the channel forever I have add a simple forever loop. This is obviously not appropriate for a real implementation, but you certainly get the point of this simple example.

That's it: this is all what you need to receive message from anywhere in the world, from any device on any platform. Beauty of PubSub pattern :)


# pubnub_send.rb

#!/usr/bin/ruby

require 'pubnub'
 
pubnub = Pubnub.new(
 :publish_key   => 'your publish key',
    :subscribe_key => 'your subscribekey',
    :error_callback   => lambda { |msg| puts "Error connecting to PubNub: #{msg.inspect}" },
    :connect_callback => lambda { |msg| puts "Connected to PUBNUB: #{msg.inspect}" }
 )

pubnub.publish(
 :channel  => 'mancusoa74demo',
    :message => !ARGV[0].nil?? Time.now.to_s + " ## " + ARGV[0] : "empty message",
 ) { |data| puts "Messaage status is #{data.response_message}" }


 sleep(1)


Similarly to sender, also to receive we need to connect to PubNub network and instantiate a Pubnub object.
Unlike the receiver, to send it is not needed to subscribe to a channel.

So in order to publish/send a message we call the publish method on the pubnub object.
You need to pass the channel name and the message you want to send.

Bam!!! as simple as that. You have now sent a message to all your subscribers around the world on different devices and platform. Again beauty of PubSub pattern :)

This model/service can be used for a tons of different purposes. Example: connect with your mobile subscribers, implement a scalable chat/messaging system, implement a scalable logging system, implement a scalable analytics system, ...

In conclusion I hope it is clear from this post that communicate with your subscribers is very simple and PubNub is offering the infrastructure to support your needs.

If you have ideas or experience how to use it in your domain let me know. I am always interested to discover new things.

Cheers

Note: In this post I discuss about pubnub service. I would like to make it very clear that I have absolutely  NO affiliation with pubsub. This is just the description of my finding while looking for cool technologies out there.