Using Stomp with Nirvana

Stomp is a very simple bi-directional text-based messaging protocol that was originally implemented by the ActiveMQ team.

While text-based wire protocols such as Stomp do not provide the same levels of performance or low latency as Nirvana’s own binary protocols, they are a popular choice for basic messaging.

In this post, we explain why one might want to integrate a Stomp client with Nirvana, and how.

Client Platform support for Stomp and Nirvana

The simplicity of the Stomp protocol has resulted in the creation of Stomp client libraries on many platforms.
The following table shows the client platforms supported natively by Nirvana, along with the client plaforms supported by Stomp (through which a Stomp client can also communicate with Nirvana):

Platform/Language Nirvana (Native) Stomp » Nirvana
J2SE yes yes
J2ME yes
(IMP, MIDP1, MIDP2)
N/A
Blackberry yes
(4.2.0 or later)
N/A
C / Dynamic C N/A yes
C++ yes yes
C# / .NET yes yes
.NET Compact Framework yes N/A
Silverlight yes N/A
Flash yes yes
Delphi N/A yes
Erlang N/A yes
haXe N/A yes
Objective-C yes yes
Perl N/A yes
PHP N/A yes
Pike N/A yes
Python N/A yes
Ruby N/A yes
SmallTalk N/A yes
MS Excel yes N/A
Javascript yes N/A
Android yes N/A
Table 1: Client language / platform support (Source: http://stomp.codehaus.org/Clients)

As shown above, Nirvana’s optimized messaging protocols, end-to-end security and guaranteed delivery are available on a wide range of client platforms.

On these platforms, Nirvana would be the obvious choice.

Nevertheless, should a developer wish to send and consume events from a platform not currently natively supported by Nirvana,
or perhaps make use of a legacy Stomp-based application, it is quite simple to enable a Stomp client to communicate with a Nirvana Realm.

How Does Stomp’s Functionality Map to Nirvana?

When we examine the functionality offered by Stomp, we can see that it supports the following commands:

  • SEND: sends a message to a destination in the messaging system
  • SUBSCRIBE: used to register to listen to a given destination
  • UNSUBSCRIBE: used to remove an existing subscription
  • BEGIN: used to start a transaction
  • COMMIT: used to commit a transaction in progress
  • ABORT: used to roll back a transaction in progress
  • ACK: used to acknowledge consumption of a message from a subscription using client acknowledgment
  • DISCONNECT: does a graceful disconnect from the server

People familiar with JMS would instantly recognize that these protocol commands can be mapped to their JMS equivalents,
and that is precisely what the StompConnect broker attempts to deliver:
a generic Stomp-to-JMS bridge that can be used against any compliant JMS implementation – including Nirvana.

Using StompConnect to bridge to Nirvana

The following steps describe how to use the StompConnect Java library to connect Stomp clients to a Nirvana Realm:

An Example Stomp Client in Ruby

To illustrate bridging between a Stomp client and a Nirvana Realm, we shall create a very simple chat client in Ruby
using the Ruby Stomp library written by Brian McCallister, now maintained by Johan Sørensen (see http://rubyforge.org/projects/stomp/).

The library is also available as a gem, so assuming you have Ruby and RubyGems installed (see http://rubyinstaller.org/) you can install it by typing:

gem install stomp

A sample chat client can then be implemented quite simply as follows (don’t forget to change the stomp_host to your matching IP):

require 'rubygems'
require 'stomp'
topic="/topic/testtopic"
stomp_host="192.168.1.1"
stomp_port=19000
puts "Welcome to the Nirvana StompConnect Chat. Enter q to quit."
print "Please enter a nickname: "
username=gets.chomp
timenow=Time.now.strftime("%d/%m/%y %H:%M")
client = Stomp::Client.new(username, "anon", stomp_host, stomp_port)
canRun=true
userInputThread = Thread.new {
  while canRun
    msg=gets.chomp
    if msg == "q" || msg =="Q"
      canRun=false
    end
    ts=Time.now.strftime("%d/%m/%y %H:%M")
    client.send(topic, "["+ts+"]"+" "+username+": "+msg)
  end
}
 client.subscribe topic, { :ack => :client } do | message |
   print "#{message.body}\n"
   client.acknowledge message # tell the server the message was handled and to dispose of it
 end
 client.send(topic, "["+timenow+"]"+" "+username+" has joined the chat") #notify others we have joined
 trap("INT") do #catch control-c signals to gracefully exit
   canRun=false
 end
 userInputThread.join #wait for the input thread to die
 client.send(topic, "["+timenow+"]"+" "+username+" has left the chat") #notify others we are leaving
 client.close #close the client object

If you have followed the steps outlined above, this simple Stomp client will now communicate with your Nirvana Realm via the StompConnect broker.

 

This entry was posted in Nirvana, Software Development and tagged , . Bookmark the permalink.

Leave a Reply

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

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

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

Facebook photo

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

Connecting to %s