Granite Data Services » Configuring GraniteDS websocket support with Flex/AIR applications

Configuring GraniteDS websocket support with Flex/AIR 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 Flex 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=flex -Dserver=servlet3

cd server-servlet3

mvn tomcat7:run-war

Now browse  to check that everything works.

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

  1. Add the websocket client dependency on the Flex application
  2. Set the Consumer/Producer to use a websocket channel
  3. Configure a Flash Policy server (mandatory for Flex clients)
  4. 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-flex/pom.xml, add the following dependency :

<dependencies>
    <dependency>
        <groupId>org.graniteds</groupId>
        <artifactId>flex-websocket-client</artifactId>
        <version>1.0</version>
        <type>swc</type>
    </dependency>
</dependencies>

2. Configure the Consumer/Producer

In client-flex/src/main/flex/chat.mxml, modify the configuration of the Consumer and Producer objects to use a websocket channel :

chatConsumer = Spring.getInstance().mainServerSession.getConsumer("chatTopic", "room", ChannelType.WEBSOCKET);
chatProducer = Spring.getInstance().mainServerSession.getProducer("chatTopic", "room", ChannelType.WEBSOCKET);

If you are using a DataObserver component, you can just change its channel before subscribing with :

myDataObserver.type = ChannelType.WEBSOCKET;

NOTE: if you are not using the Tide API (ServerSession) in your application, you will have to replace the GravityChannel by a WebSocketChannel :

channel = new WebSocketChannel("mychannel", "ws://localhost:8080/chat/websocketamf/amf");
channelSet = new ChannelSet();
channelSet.addChannel(channel);

chatConsumer = new Consumer();
chatConsumer.destination = "chatTopic";
chatConsumer.topic = "room";
chatConsumer.channelSet = channelSet;

chatProducer = new Producer();
chatProducer.destination = "chatTopic";
chatProducer.topic = "room";
chatProducer.channelSet = channelSet;

3. Configure a Flash policy server

Flash requires a specific policy server for security reasons to allow the use of network sockets.
GraniteDS provides a very basic one that can be enabled with a simple listener in web.xml :

Add a web.xml in server-servlet3/src/main/webapp/WEB-INF/web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

           version="3.0">

    <listener>
        <listener-class>org.granite.gravity.websocket.PolicyFileServerListener</listener-class>
    </listener>
    <context-param>
        <param-name>flashPolicyFileServer-allowDomains</param-name>
        <param-value>*:*</param-value>
    </context-param>

</web-app>

NOTE: If you have multiple webapps in the same server, you need only one policy server (because it must run at the root context on the specific port 843). In this case, either configure it on one webapp, or better create a specific empty webapp that only hosts the policy server.

Now you can rebuild everything, restart the server and check that the application still works.

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 with a Spring backend.

cd graniteds-tutorial-chat

mvn clean install -Dclient=flex -Dserver=spring

cd server-spring

mvn tomcat7:run-war
Tags:

Author: William

Leave a Comment