Enterprise Integration Zone is brought to you in partnership with:

Claus Ibsen is a principal software engineer from FuseSource. Claus is a full time committer on the open source integration framework Apache Camel (http://camel.apache.org) and author of the Camel in Action book (http://www.manning.com/ibsen). Claus is the most active contributor to Apache Camel and is very active in the Camel community. Claus is a frequent speaker at various conferences. Claus is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

Apache Camel now with Twitter and WebSocket

06.19.2012
| 6815 views |
  • submit to reddit
The upcoming Apache Camel 2.10 release is on its way. A lot of new components are being added, and two really exiting new components are twitter and websocket. The kick ass contributions from the community make this possible.

James Strachan and I gave a talk  a few months back at the JFokus conference in Stockholm. And for that conference I had prepared a little example using these new components. Unfortunately there was a problem with the projector and James' laptop, so we had to give the presentation with slides only.

So this weekend I found some time again, to polish the example, prepare documentation, and donate the example to the Apache Camel project.

The example is demonstrating how to poll a constant feed of twitter searches and publish results in real time using a websocket to a web page. As usual the code in Camel is very simple. All it takes is roughly..

  from("twitter://search?...")
    .setHeader("websocket.sendToAll", "true")
    .to("websocket:camel-tweet")

To use twitter, you need a twitter account. For twitter users, you may be familiar with twitter's requirement that you to grant certain applications access to your twitter account, such as twitter for iphone etc. The same applies for this example.

We have described this in more detail at the Camel twitter documentation.

When you have created an application, you get a number of details back from twitter which you need to use the twitter component. We will get back to this later.

This example is located in the examples directory of Apache Camel 2.10, in the camel-example-twitter-websocket. To run the example you use Maven with the following command: mvn compile exec:java

The example will report an error if you have not setup your twitter application. To do that, edit the source code in the src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java class.

When successfully set up, the applications starts, and you can open a web browser on http://localhost:9090/

By default we do a twitter search every 2nd seconds for the word "gaga". There is usually a lot of people tweeting about Lady gaga :). You can of course change this in the aforementioned source file.

Below is a screenshot of the example in action.

Live feeds of gaga tweets using Camel in a web browser

The client is a web browser, so we need to setup this in a .html page using JavaScript. See the src/main/resources/index.html for the source code.

The interesting piece in the source code is that it's very easy to setup a websocket client
  var location = "ws://localhost:9090/camel-tweet";

  this._ws=new WebSocket(location);
  this._ws.onmessage=this._onmessage;
  this._ws.onclose=this._onclose;

Notice that all it requires is a url to pass into the websocket constructor and then setup two functions for callbacks.
I tried using Metallica as the search term, but I must admit that there are more frequent tweets about Gaga.

Metallica tweets

Anyway, it's time to end this blog, and get the code committed. And to enjoy some Metallica music.
Published at DZone with permission of Claus Ibsen, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)