More than a decade ago, web pages were just statics. Some years ago, they were statics with some partial reloading. Now, users expect notifications, chats, events. Users expect you to provide realtime content.
Fortunately, there is a new standard for that. Having a streaming channel between the server and the client can be done using the WebSocket API.
Unfortunately, there is one major issue with realtime: scalability. Realtime means more connections, more data exchanges. Most web servers today are using threads to handle requests. Threads based networking doesn’t scale, as you will face performance and locking issues. Java web servers like Jetty, Tomcat, Glassfish etc. are using threads.
In fact, most web frameworks today will make you create web sites that are going to be deployed on thread based web servers. There is a fundamental problem here if you want to provide realtime stuff. You have to look for others technologies, and the big challenger today is NodeJS.
That’s fine, but does it means that you have to stop using your favorite web framework in order to do only NodeJS? Nop. You can employ NodeJS to just handle the realtime aspects of your applications, like broadcasting notifications. What you need then is just a bridge between NodeJS and your favorite web framework.
As I’m starting my own business, I do some Ruby now, and I started to play with NodeJS and Ruby on Rails. I made the bridge between the two platforms with Redis, using the publish/subscribe feature. Redis is a lightweight and very fast advanced key-value datastore. Benchmarks show performances about “110000 SETs/second, 81000 GETs/second in an entry level Linux box“. In fact, Redis has been designed for performance. Since Redis 2.0, there is now a publish/subscribe feature, especially designed to trap changes. Redis is supported by tons of languages, as NodeJS, Ruby and Java.
So let’s summarize what could be the global architecture. Your base application will publish events using Redis. NodeJS will subscribe to this Redis channel and as soon as an event is caught, NodeJS will relay it to subscribed end users through WebSocket API.
I wanted to experiment the same technique with Java instead of Ruby. I used the Tapestry 5 Hotel Booking application to achieve that. And here is how.