Vert.x 3 real time web apps
One of the interesting features of Vert.x is the SockJS event bus bridge. This piece of software allows external applications to communicate with Vert.x event bus using Websockets and if your browser does not support it then it gracefully degrades to pooling AJAX calls.
WebSockets bring a new level of interaction to the web, they really bring real time to web applications due to the fact that its communication model is bi-directional in contrast to the traditional HTTP model where a client can initiate a data request to a server but not the other way around.
In this small post I will demonstrate how you can create a simple collaborative drawing app. The idea is simple, all users that open the app will be be presented with a empty canvas and what they draw or is drawn on other canvas is shared in real time on their screen.
For the sake of simplicity and making this post light there is no security involved so, everyone is free to listen to what is being drawn, however the external application has limited read write access to a single address on Vert.x event bus, ensuring that other services running on the cluster will not be exposed.
This is what you should expect to see:
Bootstrap a project
If you followed the previous series on Vert.x development, you saw that Java and Maven were the main topic, since Vert.x is polyglot I will focus on JavaScript and NPM as my programming language and package management tool.
With NPM start by creating a package.json
, in order to do this we should run:
This will present a selection of questions and in the end you should have a basic package.json
file. This
configuration is very basic so you need to add a dependency to Vert.x so
you can run the application. You can add it to the dependencies
property and it should look more or less like this:
If you do not know why there is the dependency on vertx3-full
or why the added scripts
property please check the older blog post about it.
Project Structure
This post has no preference over project structure, so if you do not agree with the structure used here feel free to use what you feel best. For this example I will keep it to:
As you can imagine server.js
will be our Vert.x application and everything under webroot
will be the client
application.
The client application is not really Vert.x specific and could in theory be used by any other framework so I will go lightly over its code.
Client Application
Our application main entry point is as one can expect index.html
. In the index file define the following HTML:
As I previously wrote, the idea is to keep it as simple as possible so it is all about having a canvas element and a
application main script script.js
. All the rest are files served by CDNs that provide common web application
libraries such as jQuery
, HTML5
shim for older browsers, SockJS
client and vertxbus
bridge.
The main code is on script.js
file:
The most important part in this code is all the code related to eb
. The variable eb
is our bridge to the event
bus, Start by creating a bridge using the vertx.EventBus
object and define where to connect, using the details
of the current window location.
Then add a onopen
listener that will subscribe to the address draw
on the event bus so it can listen to all
messages regarding drawing and perform the drawing actions. Since listening is not enough I also add a mouse listener
to the document so when it moves it publishes events to the draw
address.
Note that I am using publish
and not send
, the reason should be obvious, I want everyone to know this users mouse
movements, I am not interested on sending the events to just a single user. You can see now that if you want to have
a drawing app in a one on one user basis then instead of publish()
you should use send()
.
Server Application
The server code is quite straight forward, all you need is:
We start with the usual imports, we import a reference to the Router
object and a couple of helper handlers
SockJSHandler
and StaticHandler
. As their names should tell you one handler will be responsible to handle all
SockJS
data and the other all HTTP file serving requests.
We then add then to a router and start a HTTP server that will handle all incoming request using the handler accept
function. Finally we listen on port 8080
and we are ready.
Note that there is a options object where a couple of properties are defined outbound/inbound
permitted addresses.
Without this configuration the external application will not be allowed to connect to the vert.x bus, in fact the
default configuration of the SockJSHandler is deny all. So you must specify explicitly which address are allowed to
receive messages from SockJS
and which ones are allowed to send/publish to SockJS
.
Now you can start your application, don’t forget to install the dependencies for the first time:
And then run the application:
If you now open 2 browser windows you will be able to draw nice pictures and see the drawing showing in “real time” on the other window, if you then draw on the second you should get the mirror effect on the first window.
Have fun!