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 :)

About these ads

40 Responses to Messaging using Flex and Blaze DS

  1. Solerus says:

    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. Sujit Reddy G says:

    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. AjaxSucks says:

    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. Gene says:

    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. Sujit Reddy G says:

    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. dd says:

    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. Sujit Reddy G says:

    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. better solution says:

    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. Gary says:

    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. Lucas says:

    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. Sujit Reddy G says:

    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. fx.barrett says:

    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. Sujit Reddy G says:

    Hi,

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

    Hope this helps.

  14. Noel says:

    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. Ravi says:

    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. daslicht says:

    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. Priya says:

    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. Priya says:

    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

  22. Hi Priya,

    Looks like your messaging-config.xml contains tags which are not allowed. Please try removing the tag creating problem and restart your server.

    Hope this helps.

  23. Yang says:

    Hi, Sujit. I also like to know how to get all the online (subscribed) users in real time. Is is possible to achieve this from the client side instead of having to call a java method from the server side constantly to update the subscribers information.
    If java methods have to be used, what information should I send to the client? I have read your article about sending messages from server to client but still have no idea about how to tackle this problem.
    It’d be great if you can give us a more detailed explaination about this.
    Thanks for your help

    Regards,
    Yang

  24. Christian says:

    Hello folks, well i am a little confused about the use of Flex and java, this is my problem: My java application must read two .zip files from an online server. These classes will load and parse the data in a Thread and perform certain actions depending on such info. The problem occurs when my flex application calls the java method that queries such data. I need to WAIT until the loading has finished. Any ideas?
    Another doubt is if the Java classes are being compiled and transformed into some form of ActionScript classes.
    Maybe I’m lost in space, so please I need a light in the darkness, thank you.

  25. Hi Christian,

    Java classes are not converted to AS3 directly. AMF is the format used to represent the objects to transfer between server and Flash Player. You can try not returning from the class invoked until your zip files are downloaded. You can also try pushing the data to the client when the data is ready as explained in this URL http://sujitreddyg.wordpress.com/2008/08/14/sending-messages-from-java-to-blazeds-destinations-using-messagebroker/

    Hope this helps.

  26. Arun says:

    Hi Sujit,
    Each time I click the button I am getting the error
    “Destination CreatingRpc either does not exist….”
    I know the error is quite informative but I am not able to figure out what went wrong. Would u please help me out… Thanks in advance.

    remote-config.xml
    ___________________

    org.user.UserService
    application

    services-config.xml
    ___________________

    false

    true
    4

    Thanks,
    Arun

  27. Arun says:

    remote-config.xml
    ——————
    <?xml version=”1.0″ encoding=”UTF-8″?>
    <service id=”remoting-service” class=”flex.messaging.services.RemotingService”>
    <adapters>
    <adapter-definition id=”java-object” class=”flex.messaging.services.remoting.adapters.JavaAdapter” default=”true”/>
    </adapters>
    <default-channels>
    <channel ref=”my-amf”/>
    </default-channels>
    <destination id=”CreatingRpc”>
    <properties>
    <source>org.user.UserService</source>
    <scope>application</scope>
    </properties>
    <adapter ref=”java-object”/>
    </destination>
    </service>

    “Channel” part of the services-config.xml
    _________________________________________
    <channels>
    <channel-definition id=”my-amf” class=”mx.messaging.channels.AMFChannel”>
    <endpoint url=”http://localhost:8080/blaze/messagebroker/amf” class=”flex.messaging.endpoints.AMFEndpoint”/>
    </channel-definition>
    <channel-definition id=”my-secure-amf” class=”mx.messaging.channels.SecureAMFChannel”>
    <endpoint url=”http://localhost:8080/blaze/messagebroker/amfsecure” class=”flex.messaging.endpoints.SecureAMFEndpoint”/>
    <properties>
    <add-no-cache-headers>false</add-no-cache-headers>
    </properties>
    </channel-definition>
    <channel-definition id=”my-polling-amf” class=”mx.messaging.channels.AMFChannel”>
    <endpoint url=”http://localhost:8080/blaze/messagebroker/amfpolling” class=”flex.messaging.endpoints.AMFEndpoint”/>
    <properties>
    <polling-enabled>true</polling-enabled>
    <polling-interval-seconds>4</polling-interval-seconds>
    </properties>
    </channel-definition>
    </channels>

    No other XML files is modified and the flash part is a copy paste from your blog source.

    Regards,
    Arun

  28. Hi Arun,

    Please check if your Flex Builder project is pointing to right configuration file and try running a clean command on your Flex project. Also restart your server.

    Hope this helps.

  29. Tuyen Nguyen says:

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

    [object Object]

    Answer:

    Suppose you have object Message(title,content,responseTime)

    You setup the AsyncMessage as follow in the PRODUCER:
    var aMsg:AsyncMessage=new AsyncMessage();
    msg.body.title= txtTitle.text;
    msg.body.content=txtContent.text;
    msg.body.responseTime=txtResponseTime.text;
    ….
    producer.send(msg);

    You’ll read AsyncMessage as follow in the CONSUMER:
    strTitle = msg.body.title;
    strContent = msg.body.content;
    strResponseTime = msg.body.responseTime;

    This works for me, but I feel annoying if you want to send an object which contains several objects inside it, the setup and reading information procedure is not fun at all. I wish Adobe help us the “body” attribute in AsyncMessage as Object class in Java.

    Tuyen Nguyen

  30. Kumar says:

    Can u guide if I have below scenarios :-

    GUI producer sends message to destination1 ->
    The message from destination1 is read by java class and a new message is generated and sent to destination2 -> Later GUI consumer reads from destination2 and dump in GUI.

  31. Tuyen Nguyen says:

    I think you can use Remote Object to call methods in a Java class, Kumar.

  32. francs6 says:

    Exemple for dynamic user list in chat application : http://devflex.wordpress.com/2009/10/22/flex-chat-using-blazeds/

  33. GopiSaxsi says:

    I followed the example in this: http://bit.ly/EByf5
    Now while starting Tomcat i get this error:
    MessageBrokerServlet in application ‘BlazeDS’ failed to initialize due to runtime exception: Exception: flex.messaging.config.ConfigurationException: adapter not found for reference ‘RandomDataPushAdapter’ in destination ‘RandomDataPush’.
    Not sure where I’m wrong I have checked Service-config.xml and messaging-config.xml
    Any pointers?

  34. Prabhdeep says:

    hi Sujit,this is prabhdeep I am using your chat application and its running fine.but the problem is that the message is not being sent.Can you check the error .

    **** MessageBrokerServlet in application ‘BlazeDS’ failed to initialize due to r
    untime exception: Exception: flex.messaging.config.ConfigurationException: Des
    tination ‘chat-application’ is configured to use an unsupported throttle policy,
    ‘REPLACE’.

  35. Hi Prabhdeep,

    Looks like the throttle policy is set to invalid value. Please check this for supported values http://livedocs.adobe.com/blazeds/4/javadoc/flex/messaging/config/ThrottleSettings.Policy.html

    Hope this helps.

  36. Prabhdeep Singh says:

    Hi Sujit,
    could u plz check out this error and find me any solutions for this.The error is coming whn i am creating a Java Service to connect with the MYSQL database

    MessageBrokerServlet in application ‘BlazeDS’ failed to initialize due to runtime exception: Excep
    tion: flex.messaging.config.ConfigurationException: Invalid channel endpoint URI, localhost:8400/blazeds/
    messagebroker/qosamfpolling, must begin with http.

  37. Hi Prabhdeep,

    Based on the exception details, it looks like the endpoint URL is not properly set. Please try changing your endpoint URL to “http://localhost:8400/blazeds/messagebroker/qosamfpolling” in services-config.xml.

    Hope this helps.

  38. Rajesh Reddy says:

    Hi Sujit,

    I am facing a problem , where tracing logs are not helpful in finding the issue , could you pls provide your suggestions , Jboss + BlazeDS ,

    Flex clients are connecting and disconnecting successfully to the JBOSS Topic , I have enabled logging in services-conig.xml, in the server logs requests are also received , whenever JMS message is available in the Topic its not routed to the client. There are no exceptions in the server logs also.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 112 other followers

%d bloggers like this: