Jul 28
Flex chat application that uses Apache ActiveMQ and STOMP
To follow on my last post, here is a little Flex chat application that uses Apache ActiveMQ and the ActionScript 3 STOMP library that I have been working on.
You can get the source for the application here.
You will need to download and install ActiveMQ to run this app. Installing ActiveMQ is pretty straight-forward. You can grab one of the latest snapshots here, and then follow the instructions posted here.
Here are some basic instructions to get ActiveMQ up and running:
- Download an unzip to the directory of your choice.
- Open a command-line and navigate to the bin folder inside activemq
- Run the activemq application inside the bin folder. (e.g., $ activemq/bin/activemq).
Once you’ve got ActiveMQ running. Go ahead a launch a couple instances of the Flex chat app. Login to each one and start chatting. For example,


You can also see the chat messages that come from the chat demo that comes with ActiveMQ.
In your browser, go to: http://127.0.0.1:8161/demo/chat.html, login and send a chat message. For example,



August 12th, 2007 at 10:36 pm
Wow, great stuff I’ve been looking for a good messaging server and ActiveMQ looks like it fits the bill. You going to be at Flex360 by chance? I’d like to hear more about what you think Active MQ is capible of and why you are using it vs. Fuse or Red 5.
Paul
August 30th, 2007 at 6:34 pm
Great article!!!
I have a quick question for you. I am using ActiveMQ and I have a C# program that is using client written with NMS. The client can’t see the messages that are been sent by the flex client. Neither can the flex client received messages been sent by the C# client. Yet they are both subscribing to the same topic. I guess the question is can a message sent from STOMP be received by a subscriber that is not using STOMP?
Everything that have found soo far says that it should, just wondering if am missing something.
Thanks,
Carlos
August 30th, 2007 at 8:03 pm
Just figured out that the messages been sent using stomp are been placed in a queue. While the messages been sent using NMS are been put in a topic.
September 20th, 2007 at 6:45 am
Also check out the ActiveMQ Gateway that ships with Cold Fusion 8 to really turbo on your Flex…
November 29th, 2007 at 12:19 am
another Flex chat (with video) avaible on http://www.flexvideochat.com
March 10th, 2008 at 10:03 pm
Hi,
This is a great post. I worked out all the details of a chat running on localhost.
I only had a problem when trying to open an XMLSocket with a domain, which was not localhost. I tried with a crossdomain.xml file, loadPolicyFile, nothing worked. From your experience, what should be done to be able to open an XMLSocket (the policy file is available on http://www.mydomain.com/crossdomain.xml and ActiveMQ is available on http://www.mydomain.com:61613). Thanks.
PS> The server is my computer, so anything is possible.
April 12th, 2008 at 11:55 am
Hi,
Following the last post, I am also trying to get rid of the error “security sandbox violation”, the application is not working when I try to reach over tomcat..
Nothing worked related to crossdomain.xml file suggestions, I tried so far..
any help will be great,
thanks,
ibrahim
May 7th, 2008 at 7:50 am
Hi Darren,
Thanks for the very helpful post.
Unfortunately I and apparently many others are stuck on where should we put the crossdomain.xml file
inside activemq for flash.
I have just created a RoR/ActiveMQ/Stomp/ActiveMessaging/Flex application that uses publish/subscribe
based on your tutorial and this is the last missing piece that is stopping me from making it live.
Your help would be greatly appreciated.
Many Thanks,
Aman
May 20th, 2008 at 9:16 am
Finally found the problem. The new Flash 9.0.24 had enforced new socket security policies.
Once I setup a socket server for serving socket security policy files, everything starting blowing smooth as a whistle!
Thanks for the great article!
Aman
June 6th, 2008 at 10:27 am
Hello
I have solved the security problem.
Just goto there to download a security server:http://www.flash-resources.net
But I encounter some other probelms.
It works fine when i input english.
But if i write in chinese, it said Error: Error #2002 which means that socket is invalid.
If anybody know what’s the reason, please email me: shineandsky@yahoo.cn
Thanks for your time to read my comment^_^
June 6th, 2008 at 10:41 am
Thank you , nameless hero! please help me about what’s the reason of the fault.
My fault details:
org.apache.activemq.transport.stomp.ProtocolException: content-length bytes were read and there was no trailing null byte
at org.apache.activemq.transport.stomp.StompWireFormat.unmarshal(StompWireFormat.java:149)
at org.apache.activemq.transport.tcp.TcpTransport.readCommand(TcpTransport.java:203)
at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:195)
at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:183)
at java.lang.Thread.run(Thread.java:619)
[object Object]
[Event type="ErrorEvent" bubbles=true cancelable=false eventPhase=2]
Thanks for your time^_^
August 28th, 2008 at 5:56 pm
Hi this is a great example to implement chat application.
Can you give more details on back end .i.e., i want to know the language used on server side and some details on the database.
August 28th, 2008 at 5:58 pm
Is there any possibility to implement this application using cakephp as server side language.
August 30th, 2008 at 4:16 pm
Hi Manoj,
There really isn’t a server side language involved. ActiveMQ handles all of the messaging.
Derek
August 30th, 2008 at 4:18 pm
Hi Mani,
Sorry for the delayed response. Try updating to the latest version of as3-stomp (http://code.google.com/p/as3-stomp) and see if that solve your problem.
Derek
September 16th, 2008 at 6:01 am
Hi Derek Wischusen ,
this is a great example to work on flex chat , but can you suggest me, how to configure apache ActiveMQ inside Apache Tomcat ? Please resolve my problem.
September 30th, 2008 at 5:43 pm
I cant get ActiveMQ to start on my Apache server in Linux. First I thought that it may be coz my JRE is old. But now as I have upgraded it, it still gives this error when I try bin/ActiveMQ:
The java class could not be loaded. java.lang.UnsupportedClassVersionError: (org/apache/activemq/console/Main) bad major version at offset=6
Pls help somebody.
btw the app works fine in Windows after installing and starting ActiveMQ on localhost. I really like this app
thanks Derek
October 2nd, 2008 at 4:30 pm
can somebody pls tell me where are the chat logs stored? I am having a hard time figuring this out. I also tried using JDBC myDQL drive connection using this article. http://note19.com/2007/06/23/configure-activemq-with-mysql/ this makes the database tables but not the data?? pls help
October 9th, 2008 at 8:25 pm
Hi Derek,
Do you how activemq can store the chat logs? anybody?
October 29th, 2008 at 6:51 am
Hi
I just tried running the chat example after installing activemq. I also downloaded and ran the policy server that someone mentioned above. So now i got two window command windows open running the activemq and policy servers. I refresh the chat demo page. I still get the Error #2048 security sandbox violation error. what is wrong?
March 13th, 2009 at 10:19 am
<![CDATA[
import mx.events.FlexEvent;
import flex_src.codehaus.stomp.STOMPClient;
import flex_src.codehaus.stomp.event.MessageEvent;
import flex_src.codehaus.stomp.headers.*;
[Bindable]
private var username : String;
private var stomp : STOMPClient = new STOMPClient();
private var topic : String = “/topic/CHAT.DEMO”;
private var lastSender : String;
private function init () : void
{
stomp.connect( “localhost”,8080 );
stomp.subscribe( topic );
stomp.addEventListener(MessageEvent.MESSAGE, handleMessages);
}
********** private function handleMessages(event : MessageEvent):void
{
var incomingMsg : XML = XML(event.message.body);
var from : String = String(incomingMsg.@from);
switch (String(incomingMsg.@type))
{
case “join”:
conversation.htmlText += “” + from +” has joined the room\n”;
break;
case “chat”:
if (from == lastSender)
{
from = “… “;
}
else
{
lastSender = from;
from +=”: “;
}
conversation.htmlText += from + String(incomingMsg) + “\n”;
break;
case “leave”:
conversation.htmlText += “” + from +” has left the room\n”;
break;
default:
trace(’unknown message type’);
break;
}
}
private function doLogin () : void
{
username = txt1.text;
var joinMsg : XML =
stomp.send(topic,joinMsg.toXMLString());
this.setCurrentState(’Chat’);
txt1.text=”;
}
private function doSend () : void
{
var chatMsg : XML = {txt1.text}
stomp.send(topic,chatMsg.toXMLString());
txt1.text=”;
}
]]>
i am getting error in the code which i am stared know there
like
type was not found
what i can do for that
March 13th, 2009 at 10:21 am
how to run the Activemq
and what we hav to download for STOMP to use in this chat application
March 20th, 2009 at 7:33 pm
I get the missing trailing null byte – error when sending a first message too (like Mani). When I try to update the as3stomp-library
it cant find the messageEvent
(type not found in
private function handleMessages(event : MessageEvent) : void
)
What to do???
Any other/better options to get realtime in Flex/PHP?
March 20th, 2009 at 8:00 pm
Hi Stevie,
The app in this post uses a very old version of the Stomp client. I’ll post an updated version this weekend. You might also want to check out the demo app that is on the project site: http://as3-stomp.googlecode.com/files/StompImageShare.zip
March 20th, 2009 at 8:25 pm
OK, we have to change the code of the chat appl. according to new conventions of the as3stomp-lib. but the problem of the missing null byte is still in there. I am monitoring the chat with the php stomp client from codehaus. It crashes because of the missing null byte. It echos only the first login message. How could we send a null byte from within AS3? And how could we query against ActiveMQ from within AS3 to write a flex monitor?
Any suggestions?
March 20th, 2009 at 8:47 pm
Hi Stevie,
The ActionScript STOMP client terminates every message with a null byte. I thought I remembered reading something about this issue on the RabbitMQ mailing list: http://www.nabble.com/Stomp-frame-not-terminated-with-null-character-td19447635.html. It looks like this issue is specific to the PHP client.
March 20th, 2009 at 9:41 pm
Hello Derek,
Thank you for the tip…I will give it a try.
Do you have any suggestions for querying against ActiveMQ from within AS3 to retreive status informations?
March 20th, 2009 at 9:44 pm
…or having any alternative ideas to php-stomp for a flex/php solution?
March 21st, 2009 at 12:05 am
Hi Derek,
it seems that your null byte is not there null byte…
they do something like this which works using only php-clients:
// ActiveMq
if($data[$len - 2] == “\x00″ && $data[$len - 1] == “\n”) {
$continue = false;
}
I believe they append a “\n” to the null byte looking to there send method:
return $data .= “\x00\n”;
How can I append this “\n” in your as3-lib.? I tried this:
transmission.writeByte( NULL_BYTE+’\n’ );
But it did not work.
Any solutions?
March 21st, 2009 at 2:21 am
try
transmission.writeByte(NULL_BYTE);
transmission.writeUTFBytes(’\n’);
March 21st, 2009 at 3:48 am
hm takes no effect, are there other encodings for ‘\n’ in AS3?
March 21st, 2009 at 3:14 pm
Now I have two stomp-client-implementations running… talking with clients/instances of there own library, build in the same language very well, but not with clients build with another library/language. Actually I would think the stomp-conventions have to be so clear that every instance should be able to talk and listen to any valid external stomp-client, regardless to there implementation.
Very very strange
.
My goal is to have realtime/server push without java and write the business logic on server in php. Did someone heard something about a BlazeDS/PHP aproach?
Are there some benchmarks/experiences about the stomp/php client from codehaus. It seems that this is a critical point and the lib is poorly documentated/maintained.
I found an alternative php-stomp-client here
http://github.com/felixjendrusch/stomp/tree/master
but its not finished jet and there is no manual.
Any suggestion will be appreciate.
March 22nd, 2009 at 5:14 pm
The hint:
stomp.sendTextMessage(topic,msg);
March 22nd, 2009 at 6:58 pm
That method is for sending a TextMessage (rather than the default BytesMessage) with ActiveMQ. Basically all it does is exclude the content-length header. Perhaps the PHP client does not work with BytesMessags.
March 22nd, 2009 at 7:54 pm
Thx Derek,
…another question. Can I send file-data using your as3-stomp lib and ActiveMQ?
March 23rd, 2009 at 2:38 am
Any ideas to implement this:
http://activemq.apache.org/advisory-message.html
or how to start using your as3-lib/chat-app?
March 31st, 2009 at 3:50 am
Thanks for the sample!
same nebie question: I installed my ActiveMQ server on another box with a stactic IP, at the HTTP Server root, I placed the “crossdoain.xml” there – http://12.123.123.12/crossdomain.xml shows:
When I run the “swf” or “html” locally, it works fine. But when I copy those to my Yahoo hosted WebServer, it did not work – with no error.
Thanks for your help!