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

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 :


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();

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=""




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 :


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

Author: William

Leave a Comment