Flex Chat using Blazeds

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 is fully capable of handling dynamic user list and implements login/logout functionality. I have not included any error handling stuff to keep the code as simple as possible.

Thanks to Mr Sujit Reddy, who laid the base and I have extended that here.

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.

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>

</server>

</properties>

</destination>

</service>

remoting-config.xml

<destination id=”MySessionHandler” channels=”my-amf”>

<properties>

<source>MySessionHandler</source>

</properties>

<adapter ref=”java-object”/>

</destination>

 Here is the Java code which is working as backbone for this app: MySessionHandler.java

import flex.messaging.FlexContext;
import flex.messaging.FlexSession;

public class MySessionHandler {

 private FlexSession mySession;
  
 public MySessionHandler()
 {
  mySession= FlexContext.getFlexSession();
  if(mySession.getAttribute(“myCounter”) == null)
  {
   mySession.setAttribute(“myCounter”, new Integer(1)); 
  }
  
  if(mySession.getAttribute(“userName”) == null)
  {
   mySession.setAttribute(“userName”, “admin”); 
  }
 }
 
 public String getUserName()
 {
  String userName = null;
  userName = (String)mySession.getAttribute(“userName”);
  return userName;
 }
 
 public void addUserName(String userName)
 {
  String userN = null;
  userN = (String)mySession.getAttribute(“userName”);
  userN = userN+”,”+userName;
  mySession.setAttribute(“userName”, userN);
 }
 public void removeUserName(String userName1)
 {
  String userN1 = null;
  userN1 = (String)mySession.getAttribute(“userName”);
  String delimiter=”,”;
  String[] arr;
  arr = userN1.split(delimiter);
  String userN=null;
  for(int i=0;i<arr.length;i++)
  {
   if(!userName1.equals(arr[i]))
   {
    
    if(userN == null)
    userN = arr[i];
    else
    userN = userN+”,”+arr[i];
   }
   
  }
  mySession.setAttribute(“userName”, userN);
 }

}

Upto this..we have done with Server setup and Java backend logic. Now  we will prepare Client code for actual Flex chat which is very tricky as there are no direct way for implementing dynamic users list and there status,in Blazeds. However things are working pretty well for me..you guys can give a try with this.

Please download the MXML file from this link:

flexChat.mxml

Thats it..!!

 

Cheers…

dev

2 Responses

  1. Hi,

    What does the REPLACE policy option in your throttle-outbound node do?

    It’s the first time I’ve seen that option and I couldn’t find anything mentioning it in the BlazeDS developer guide:

    http://livedocs.adobe.com/blazeds/1/blazeds_devguide/help.html?content=messaging_7.html

    Thanks!

    • Hi Stu,
      The throttle-outbound property is used for controlling messages max-frequency, and the max-frequency attribute controls how many messages per second the server can send. The policy attribute indicates what to do when the message limit is reached.
      A policy value of ERROR indicates that an error should be returned if the limit is reached. A policy value of IGNORE indicates that no error should be returned if the limit is reached. A policy value of REPLACE indicates that the previous message should be replaced when the limit is reached.

      Cheers..!
      dev

Leave a Reply