Messaging using Flex and Blaze DS

The BlazeDS messaging capability is based on established messaging standards and terminology. BlazeDS messaging provides a client-side API and a corresponding server-side Message Service (BlazeDS Message Service) for creating BlazeDS messaging applications. BlazeDS messaging also enables participation in Java Message Service (JMS) messaging.

There are two components available in the Flex frame work for messaging, mx:Producer and mx:Consumer. Producer is the component which is used for producing messages to a destination and Consumer is used for subscribing to a destination and receiving messages published to that destination. Consumer also gives option to filter the messages based on user defined constraints.

I have created a chat application using Flex and Blaze DS. Flex application will use the publish-subscribe messaging mechanism. Flex application checks for new messages using polling mechanism.

This chat application will send messages to selected users only. I have not included any error handling stuff to keep the code as simple as possible. :)

We just need to create the client, the server side message handling is provided by Blaze DS. We create an mxml file, which will handle the client logic and configure a destination on the server.

BlazeDS should be setup and running to execute this sample. Please follow the steps in http://sujitreddyg.wordpress.com/2008/01/14/invoking-java-methods-from-adobe-flex/ to set up the BlazeDS and setting up a Flex application, which is mapped to BlazeDS root directory.

Chat application snapshots

geeva chat client

murali client

Chat Client

Please download the mxml file from this URL: http://sujitreddy.g.googlepages.com/ChatApplication.mxml

Configuring destination on the server

Navigate to tomcat/webapps/blazeds/WEB-INF/flex under BlazeDS Setup folder and open the file messaging-config.xml. Replace the XML file content with the content below. The ID of the destination added below will be used by the components at the client side.

messaging-config.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<service id=”message-service”

class=”flex.messaging.services.MessageService”>

<adapters>

<adapter-definition id=”actionscript” class=”flex.messaging.services.messaging.adapters.ActionScriptAdapter” default=”true” />

</adapters>

<default-channels>

<channel ref=”my-polling-amf”/>

</default-channels>

<destination id=”chat-application”>

<properties>

<network>

<session-timeout>0</session-timeout>

<throttle-inbound policy=”ERROR” max-frequency=”50″/>

<throttle-outbound policy=”REPLACE” max-frequency=”500″/>

</network>

<server>

<max-cache-size>1000</max-cache-size>

<message-time-to-live>0</message-time-to-live>

<durable>true</durable>

<durable-store-manager>flex.messaging.durability.FileStoreManager</durable-store-manager>

</server>

</properties>

</destination>

</service>

That’s it. Your chat application is ready for use. :) Adobe ROCKS :)

21 Responses to this post.

  1. Posted by Solerus on February 7, 2008 at 4:31 pm

    This is a great solution. But what if you wanted to communicate with a data base using messaging? What would you recommend for the ‘middle-man’ to receive the new data from a flex app and then push it out to the subscribing flex app?

  2. Posted by Sujit Reddy G on February 8, 2008 at 12:15 pm

    You can create custom message service adapters. :) It is very easy :) You just have to extend ServiceAdapter class and override the invoke() method. Check this URL for more information

  3. Posted by AjaxSucks on March 8, 2008 at 11:43 pm

    I’d be pretty impressed if you had this working with a dynamic chat list, i.e. a standard chat room. Seems that it’s a little tricky doing this with Blaze (rather than LCDS) when you don’t have access to the [Managed] functionality. It might sound easy, but when multiple users login to the room all at the same time it’s tricky keeping everyone’s chat user list up to date. User timeouts/disconnects are also a problem – I’ve still been unable to detect when a client drops despite trying a few different methods.

  4. Posted by Gene on March 13, 2008 at 4:09 pm

    What do you use if you do not want to use Tomcat as your webserver?
    I am forced to use IIS because I am on a DoD networkl. Is there a BlazeDS config for IIS?

    Thanks in advance,
    Gene

  5. Posted by Sujit Reddy G on March 14, 2008 at 10:53 am

    Hi Gene,
    BlazeDS is a J2EE application. You need a servlet container to have BlazeDS running. You can find out how to integrate Java Servlet container in IIS web server. Please check out the URL below for more details on running J2EE apps on IIS.
    http://support.microsoft.com/kb/q240842/

  6. hey it’s cool but the user list being hard coded is really not sufficient because it should be checking on the server for who is subscribed (online). How would you do that?

  7. Posted by Sujit Reddy G on March 25, 2008 at 5:15 am

    Hi DD,

    As we are using BlazeDS. I would expose a method on the server which will give me the list of users online at a particular point of time. I will keep polling to that method and keep getting the latest information of online users. :) If any one else has better solutions (with BlazeDS), please share :)

  8. Posted by better solution on March 28, 2008 at 4:08 pm

    A slightly better solution would be to subscribe to a feed service that “feeds” the current list of usernames. This at least avoids the client having to initiate the update, now it’s one way only.

  9. Posted by Gary on September 4, 2008 at 2:51 pm

    I assume you’re not using Flex 3 and BlazeDS. You have a variable in your messaging-config.xml file that is not supported in Flex 3 BlazeDS. flex.messaging.durability.FileStoreManager That will generate an error when you start the server.

    Also, durable=true doesn’t work without JMS

  10. Posted by Lucas on September 17, 2008 at 2:54 pm

    Hi!
    Do You know, how to do chat with online user list??
    I know that I need to use MessageService.getSubscriberIds but
    problem is, how to do this?? Could someone help me??

  11. Posted by Sujit Reddy G on September 18, 2008 at 1:48 pm

    Hi Lucas,

    I would have exposed a Java method as Remoting destination and let the Java method pass the details required. You can now invoke that Java method from Flex application.

    You can also use the session to maintain the list of users logged in. Please find more details on session management at the URL below.

    http://sujitreddyg.wordpress.com/2008/05/16/session-data-management-in-flex-remoting/

    Hope this helps :)

  12. Posted by fx.barrett on January 4, 2009 at 1:58 am

    A really dumb question but I’m a total noob to Tomcat. How would you deploy this chat application on the web ? Should you hosting plan include Tomcat ? Or can you install it or something and after that install BlazeDS too ?

    Thanks guys.

  13. Posted by Sujit Reddy G on January 5, 2009 at 6:41 am

    Hi,

    You need Tomcat to have BlazeDS support. Tomcat or any J2EE container is sufficient to add BlazeDS support.

    Hope this helps.

  14. Posted by Noel on February 22, 2009 at 8:21 pm

    Hi sujit,

    when i select a user and type in a message and click Send all i get is a popup which says

    [object Object]

    any idea whats wrong?

  15. [...] into modulesSession data management in Flex RemotingMapping Action Script objects to Java objectsMessaging using Flex and Blaze DSBlazeDS and LCDS Feature differenceRendering PDF content in Adobe AIR applicationHandling Java [...]

  16. Hi Noel,

    try the body property.

  17. Posted by Ravi on June 17, 2009 at 10:53 am

    Hi sujit,

    when i select a user and type in a message and click Send all i get is a alert saying
    [object Object]

    i checked in fire bug “http://localhost:8080/samplewebapp/messagebroker/amfpolling” this is giving 404 error.. Is this the reason??
    How to solve this

  18. Hi Ravi,

    What is that you are passing to the show function of the Alert class? make sure you pass the right property, body is the property which has the data returned.

    Hope this helps.

  19. Posted by daslicht on June 19, 2009 at 4:49 pm

    Hi,

    how could we get all subscribed users? I saw “getSubscriberIds” in the javadoc but have no clue how to use it yet. Some help would be very very very nice, please :D

  20. Posted by Priya on July 6, 2009 at 1:51 pm

    Hi

    Nice article.
    I tried setting up the chat application and it showed up the UI fine, but the message is not getting sent.
    I get an exception:
    **** MessageBrokerServlet failed to initialize due to runtime exception: Exception: flex.messaging.config.ConfigurationException: Unrecognized tag found in . Please consult the documentation to determine if the tag is invalid or belongs inside of a different tag:
    ‘/jms’ in destination with id: ‘chatApplication’ from file: messaging-config.xml
    at flex.messaging.config.MessagingConfiguration.reportUnusedProperties(MessagingConfiguration.java:682)
    at flex.messaging.MessageBrokerServlet.init(MessageBrokerServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4045)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4351)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

    Jul 6, 2009 1:50:01 PM org.apache.catalina.core.ApplicationContext log
    INFO: Marking servlet MessageBrokerServlet as unavailable
    Jul 6, 2009 1:50:01 PM org.apache.catalina.core.StandardContext loadOnStartup
    SEVERE: Servlet /BlazeDSPOC threw load() exception
    javax.servlet.UnavailableException: Unrecognized tag found in . Please consult the documentation to determine if the tag is invalid or belongs inside of a different tag:
    ‘/jms’ in destination with id: ‘chatApplication’ from file: messaging-config.xml
    at flex.messaging.MessageBrokerServlet.init(MessageBrokerServlet.java:170)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4045)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4351)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
    Jul 6, 2009 1:50:01 PM org.apache.coyote.http11.Http11Protocol start
    INFO: Starting Coyote HTTP/1.1 on http-8080
    Jul 6, 2009 1:50:01 PM org.apache.jk.common.ChannelSocket init
    INFO: JK: ajp13 listening on /0.0.0.0:8009
    Jul 6, 2009 1:50:01 PM org.apache.jk.server.JkMain start
    INFO: Jk running ID=0 time=0/31 config=null
    Jul 6, 2009 1:50:01 PM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 4300 ms

    Please help.

    Thanks
    Priya

  21. Posted by Priya on July 6, 2009 at 1:51 pm

    Hi

    Nice article.
    I tried setting up the chat application and it showed up the UI fine, but the message is not getting sent.
    I get an exception:
    **** MessageBrokerServlet failed to initialize due to runtime exception: Exception: flex.messaging.config.ConfigurationException: Unrecognized tag found in . Please consult the documentation to determine if the tag is invalid or belongs inside of a different tag:
    ‘/jms’ in destination with id: ‘chatApplication’ from file: messaging-config.xml
    at flex.messaging.config.MessagingConfiguration.reportUnusedProperties(MessagingConfiguration.java:682)
    at flex.messaging.MessageBrokerServlet.init(MessageBrokerServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4045)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4351)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

    Jul 6, 2009 1:50:01 PM org.apache.catalina.core.ApplicationContext log
    INFO: Marking servlet MessageBrokerServlet as unavailable
    Jul 6, 2009 1:50:01 PM org.apache.catalina.core.StandardContext loadOnStartup
    SEVERE: Servlet /BlazeDSPOC threw load() exception
    javax.servlet.UnavailableException: Unrecognized tag found in . Please consult the documentation to determine if the tag is invalid or belongs inside of a different tag:
    ‘/jms’ in destination with id: ‘chatApplication’ from file: messaging-config.xml
    at flex.messaging.MessageBrokerServlet.init(MessageBrokerServlet.java:170)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4045)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4351)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
    Jul 6, 2009 1:50:01 PM org.apache.coyote.http11.Http11Protocol start
    INFO: Starting Coyote HTTP/1.1 on http-8080
    Jul 6, 2009 1:50:01 PM org.apache.jk.common.ChannelSocket init
    INFO: JK: ajp13 listening on /0.0.0.0:8009
    Jul 6, 2009 1:50:01 PM org.apache.jk.server.JkMain start
    INFO: Jk running ID=0 time=0/31 config=null
    Jul 6, 2009 1:50:01 PM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 4300 ms

    Please help.

    Thanks
    Priya

Respond to this post