So I just wrote up a Websocket server using CometD/Bayeux. It’s a ridiculously simple app - but went quite a long way in helping to understand the nitty gritties with putting up a Websocket server and CometD/Bayeux. Thought that I’ll put it up for reference - should help in getting a leg up on getting started with CometD.
The sample’s up on github at https://github.com/raghur/rest-websocket-sample
Here’s how to go about running it:
-
clone the repo above
-
run mvn jetty:run
-
Now browse to http://localhost:8080 to see the front page
-
There are two parts to the app
-
A RESTful API at http://localhost:8080/user/{name} - hypothetical user info - get retrieves a user, put creates a user and delete obviously deletes the user.
-
The websocket server at localhost:8080/cometd has a broadcast channel at /useractivity which receives events whenever a user is added/deleted. The main page at http://localhost:8080 has a websocket client that updates the page with the user name whenever a user is added or removed.
And here’s the nuts and bolts:
-
BayeuxInitializer - initializes the Bayeux Service and the EventBroadcaster. Puts the EventBroadcaster in the servlet context from where the RESTful service can pick it up to broadcast.
-
EventBroadcaster - creates a broadcast channel in the ctor. Provides APIs to publish messages on this channel.
-
HelloService - basic echo service taken from Maven archetype
-
MyResource - the RESTful resource which responds to GET/PUT/DELETE - nothing major here. If a user is added or deleted, then it pushes a message on the broadcast channel by getting the EventBroadcaster instance from the servlet context.
It’s about as simple as you can get (beyond a Hello world or a chat example). Specifically, I wanted a sample where back end changes can be pushed to clients.