Granite Data Services » Configuring GraniteDS websocket support with JavaFX applications

Configuring GraniteDS websocket support with JavaFX applications

The current Getting Started tutorials use the default long polling transport for messaging, but it’s quite easy to setup websockets instead.

Let’s take the as a simple starting point. First we clone, build and run the default application with a JavaFX client and a basic Servlet 3 container :

git clone http://github.com/graniteds-tutorials/graniteds-tutorial-chat.git

cd graniteds-tutorial-chat

mvn clean install -Dclient=javafx -Dserver=servlet3

cd ../server-servlet3

mvn tomcat7:run-war

And start the JavaFX application in another window :

cd client-javafx

mvn jfx:jar

java -jar target/jfx/app/chat.jar

To configure websocket for this application, there are a few steps required :

  1. Add the websocket client dependency on the JavaFX application
  2. Set the Consumer/Producer to use a websocket channel
  3. When using a Servlet 3 configuration, the JSR 356 support is automatically detected and enabled. When using Spring, it is necessary to enable it manually in web.xml

1. Add the websocket client dependency

In client-javafx/pom.xml, add the following dependency :

<dependencies>
    <dependency>
        <groupId>org.eclipse.jetty.websocket</groupId>
        <artifactId>websocket-client</artifactId>
        <version>9.1.5.v20140505</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty.websocket</groupId>
        <artifactId>javax-websocket-client-impl</artifactId>
        <version>9.1.5.v20140505</version>
    </dependency>
</dependencies>

This is the dependency for the Jetty 9 websocket client. You can alternatively use :

<dependency>
    <groupId>org.glassfish.tyrus.bundles</groupId>
    <artifactId>tyrus-standalone-client-jdk</artifactId>
    <version>1.8.2</version>
</dependency>

2. Configure the Consumer/Producer

In client-javafx/src/main/java/ChatClient.java, modify the configuration of the Consumer and Producer objects to use a websocket channel :

final Consumer chatConsumer = serverSession.getConsumer("chatTopic", "room", ChannelType.WEBSOCKET);
final Producer chatProducer = serverSession.getProducer("chatTopic", "room", ChannelType.WEBSOCKET);

NOTE: if you are using a DataObserver in your application, it can be configured with:

new DataObserver(ChannelType.WEBSOCKET, serverSession)

That’s all if you are using the Servlet 3 configuration (with @ServerFilter).
You can rebuild and restart the server and client applications.

4. (Spring only) Configure JSR 356 support in web.xml

The Spring support does not (yet) detect and setup the JSR 356 support. You will also have to add the following listener to your web.xml :

<listener>
    <listener-class>org.granite.gravity.websocket.GravityWebSocketDeployer</listener-class>
</listener>

Now you can rebuild everything and restart the server and client with a Spring backend.

cd graniteds-tutorial-chat

cd server-spring

mvn clean install -Dserver=spring

mvn tomcat7:run-war
Tags:

Author: William

Leave a Comment