WEBSOCKET, BIG DATA, AND NON-BLOCKING SERVER-SIDE I/O WITH KAAZING, MONGODB, AND NODE.JS
If you add a server-side component to it, you can do some central coordination and processing. And if on top of that you use a persistent storage, you can record all your real-time interactions to the database, and if needed play it all back. Icing on the cake is the ability of setting the replay speed: speeding it up or slowing it down.
Here’s what we did:
Read on to see how we did it.
Now let’s see how it’s all done.
Installing Kaazing and the Multi-Client WebSocket Drawing App
The simplest way of getting started is by installing the WebSocket app. To do so, you need to get a WebSocket server. You can download, install, and start a fully functional unlimited developer licensed Kaazing WebSocket Gateway in a few minutes. Kaazing comes in various flavors (editions). As mentioned above, we use the JMS Edition, bundled with the open source Apache ActiveMQ JMS message broker, giving us the power of messaging on top of HTML5 WebSocket. Once you have the WebSocket server and the message broker up and running, all you need to do is copy the application code into a sub-directory:
$GATEWAY_HOME/web/extras. We called ours
draw. You can download the application from github.
After starting up the Kaazing WebSocket Gateway, you’ll be able to access the application through the following URL:
At this point, you have a collaborative mini drawing app. The app uses simple color coding: red is used to display drawing happening in this browser, and yellow is used when the drawing is originating from somewhere else, that is another browser window or played back from the database.
Now, let’s walk through how the app was built.
HTML5 App in the Browser
The client-side HTML5 app consists of a couple of simple files:
The HTML file renders the visible objects (canvas, button, and speed slider) on the page, the CSS file sets the visual attributes for them.
The draw.js file contains all the application logic. On the one hand it detects mouse gestures and takes care of the drawing, on the other hand it handles the communication with the message broker: it publishes messages when you’re drawing on the canvas, and consumes messages when somebody else is drawing or when a previously recorded drawing is played back from MongoDB and Node.js.
The browser can send two “types” of messages: control messages and drawing messages. Control messages are startRecording, stopRecording, playBack, and deleteRecording.
If you’d like to learn more how you can use WebSocket with pub/sub messaging to build collaborative apps, this Tutorial is a great starting point.
Server-Side Code in Node.js
The app running in Node.js communicates with the message broker through STOMP. The server-side code is located in
server.js. Before starting it, make sure you have Apache ActiveMQ running, as the server is attempting to connect to upon start. To start the server, simply run:
$ node server.js
After successfully connecting to it, the app will subscribe to drawTopic (
/topic/drawTopic) that the browsers are publishing to. When the browser sends a control message, Node.js works hand-in-hand with MongoDB.
When recording, we save the coordinates of the pixels, along with the timestamp. Saving the timestamp will allow us to play back the recording in a properly timed manner (longer pauses in the drawing are recorded and played back as such).
The play-back functionality is done by Node.js querying the contents of the database (pixels to be drawn), and then scheduling the messages for publishing through the message broker. The messages will then eventually result in actual pixels drawn on the canvas(es) in the browser(s). We use the timestamp to set the delay between the messages.
Deleting the Recording
We can very easily delete the previous recording by deleting the collection from MongoDB.
- Check out the project on github.
- Try following the tutorial for building a simple peer-to-peer application yourself.
- Learn more about Kaazing WebSocket Gateway – JMS Edition, which is a messaging over WebSocket solution using the Java Message Service API.
- Check out the documentation and other how-tos for building clients using Kaazing WebSocket Gateway – JMS Edition.