Spring Boot, Websockets, StompJS and AngularJS – a few notes

I recently did a spike to get a Groovy-based Spring Boot project to communicate with an AngularJS/Yeoman front-end via the Websockets/SockJS support provided by Spring.

Here are a few resources and problems I ran into while integrating the project.

Websockets and Spring

There is a guide in the Spring.io site around websockets, but I found the guide to be very poor specially when it comes around testing and different available strategies.

A much better resource is Rossen Stoyanchev’s talk at the SpringOne2GX, available on YouTube.

The sample project for this video on Github is specially helpful since it provides three different testing strategies at different levels of integration – without Spring Integration, with Spring Integration and spinning up a full container.

Spring Boot and Websockets

We ran into an issue with the default starter for websockets. Basically, schedulers would fail if you have more than one TaskScheduler registered. If you’re using the Stomp support, you get two task schedulers registers and this caused our application to fail whenever the Spring Context Reloaded.

There is a StackOverflow issue that best describes the issue we ran into.

The solution, as described in this Spring JIRA, is to disable Spring Boot’s Websocket auto configurer when you want to use STOMP.

To use websockets, you ideally want to use Tomcat 8. Unfortunately, there doesn’t seem to be a mechanism to specify a new Tomcat version in Spring Boot projects using the provided Tomcat Starter. To work around the problem, you can define the dependencies defined in this POM.xml , but with 8.0 version numbers to your build.gradle file. Spring Boot’s reliance on parent POMs makes it very hard to take their gradle support seriously.

Yeoman, AngularJS and StompJS

We used an AngularJS project that is built by Yeoman as our front end.

While there is an AngularStomp project on Github, we found it more intuitive to port the Socket.io AngularJS provider written by Brian Ford to use the StompJS library. Ford’s project had more examples of automated testing and provides simpler mechanisms for overriding clients.

As Yeoman projects use bower for dependency management and the stompjs library does not include a proper .bower file, we found that we had to include StompJS via the following command:

bower install https://raw.github.com/jmesnil/stomp-websocket/master/lib/stomp.min.js

This makes bower deal with the dependency correctly.

3 thoughts on “Spring Boot, Websockets, StompJS and AngularJS – a few notes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s