About Claus Ibsen

Claus Ibsen is a principal software engineer from Red Hat. Claus is working full time as Apache Camel committer. And is author of the "Camel in Action" book.

Camel: Now with Twitter and Websocket

The upcoming Apache Camel 2.10 is well underway. A lot of new components is being added, and two really exiting new components is twitter and websocket. Its yet another kick ass contributions from the community that makes this possible.

James Strachan and I gave a talk last month 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 web socket 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 which have setup an application to be used. For twitter users, you may be familiar that twitter requires you to grant applications access to your twitter account, such as twitter for iphone etc. The same applies for this example.

We have described this in more details 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 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 setup the applications starts, and you can open a web browser on page http://localhost:9090/

By default we do a twitter search every 2nd second 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 its 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 an url to pass into the WebSocket constructor. And then setup two functions for callbacks.
Have fun, and I tried using Metallica as the search term, but I must admit that there are more frequent tweets about Gaga.

Metallica tweets
Anyone its time to end this blog, and get the code committed. And to enjoy some Metallica music, and have a nice weekend.

Reference: Camel now with twitter and websocket from our JCG partner Claus Ibsen at the Claus Ibsen riding the Apache Camel blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


two + 9 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.