DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Apache Camel now with Twitter and WebSocket

Apache Camel now with Twitter and WebSocket

Claus Ibsen user avatar by
Claus Ibsen
·
Jun. 20, 12 · Interview
Like (0)
Save
Tweet
Share
9.68K Views

Join the DZone community and get the full member experience.

Join For Free
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.
twitter Apache Camel WebSocket

Published at DZone with permission of Claus Ibsen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Boot, Quarkus, or Micronaut?
  • Introduction to Containerization
  • Introduction to Container Orchestration
  • Stop Using Spring Profiles Per Environment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: