How to use the official Java websocket API just for the websocket part

Hi!

This will be my first article for the Dobby project. With ClĂ©ment, we’ve been discussing about software design and how do we want to split responsibilities into different modules. But, here isn’t the question, I will discuss this point in a next post. In fact, we’ve come to the need to implement a very simple websocket server to make our frontal application connected with our things. We’ve experienced some trouble with the first implementations of the websocket protocol in Ruby (with our dirty project Ideologia) and we don’t want to reproduce the same schema by choosing any third-party library without support.

So, the idea that came to us was to use the standard Java implementation. The destination language is almost fixed, even if we already have the idea to move on with Erlang… The idea to choose an officially supported library was led by the need of a stable stack with which we can play the way we want. But, that wasn’t so easy to do. I was about to give up on the idea of this implementation when I’ve seen this post on StackOverflow: Using Java Websocket API Implementation without a Web-Server.

To have a better understanding of the domain, Java has a standard implementation which is used by the trio Tyrus-GlassFish-Grizzly. They made something very interesting since you can deploy a JSP+websocket application in few time. But in our case, we don’t need the JSP part nor the whole server application packaging: we want to run the server ourself! It’s simple as that: we want our main() method back!

The last response on the StackOverflow post was pointing at a test with Tyrus on GlassFish, no matter what it says, the interesting thing here is the start() method! And, in accordance with this last response on StackOverflow, it’s running in a standalone mode, so, without a GlassFish running. That was my last experiment with that solution, after trying to trick the system in various dumb ways, I’ve copied this code (and the associated EndPoint) on my machine, created a new project on my favorite Java IDE and tried hard to solve the dependencies. My only wich was to to be capable to press the Run button of my IDE and see the websocket running without JSP nor application server. I finally found the good set of dependencies. It’s simple as this packages :

    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-server</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-grizzly</artifactId>
        <version>1.2.1</version>
    </dependency>

We are now building our frontal server with this. And you can see a minimal project with this approach on my GitHub : simple-java-websocket

And you, what do you think about this choice of this implementation? Maybe there is a simpler choice that is not so cumbersome! Please, feel free to share your thought on this question.

One thought on “How to use the official Java websocket API just for the websocket part”

Leave a Reply

Your email address will not be published. Required fields are marked *