Flash Media Server Text Chat Application

I recently began a new project at work to create a text application for a support service.  I wanted to use Flex 2 with the new Flash Media Server 2.  I am pretty familiar with Flash Communication Server and have Brian Lessers’ Programming Flash Communication Server.   However, with Flex 2, things have change a bit and it took me a while to find a suitable example application (Adobe’s examples are way out of date).

Brian Lesser’s site had an updated example that worked for video chat, but it sent messages along the NetStream. I wanted my application to synchronize with the Remote Shared Object, similar to the the  Text Chat application found in the development center.

For this example, I modified Brian’s example and will only discuss what I did different to get the text chat to work for me.  Flash Media Server 2 (and Flash Communication Server) only understand AMF0, rather than the new AMF3 used with Flex 2.  You must write the backward code for bot the NetConnection and the SharedObject:

NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
SharedObject.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;

The application I am building needed to keep track of users so I passed along a username parameter on the net connection:

nc.connect("rtmp://localhost/chat_test", userName.text);

I didn't want my Remote Shared Object to be persistent, so I just connected using the default connection parameters on my shared object:

ro = SharedObject.getRemote("ChatUsers", nc.uri);

In order to have the callback methods talk to the public methods in the application.  I could have also set the callback object to any object I wanted.

nc.client = this;
ro.client = this;

In the FlexFCS.mxml file I created several public functions to receive the callback methods NetConnection and SharedObject calls:

/** showMessage is the function called by the SharedObject. */
public function showMessage(msg:String):void{
writeln("showMessage: " + msg + "\n");
}
/** setHistory is the called when the client first connects */
public function setHistory(msg:String):void{
writeln("showHistory: " + msg + "\n");
}
/** msgFromSrvr receives any broadcast messages from connected clients */
public function msgFromSrvr(msg:String):void{
writeMessage(msg);
}

The main.acs file on the Flash Media Server is pretty much the same as the one found in the Text Chat application on Adobe's site.  I have added a prototype function to the client at the top of the ACS file to get the local server time and changed the name of the SharedObject.

You can download the the complete file here: FlexChat.zip

-Mr

About these ads

58 thoughts on “Flash Media Server Text Chat Application

  1. Pingback: Thanks, Mister! » FMS, Cairngorm, and Flex

  2. How can make that message to be send only between two clients?
    With your example message are send to all clients that is connected to the sgared object “ChatUsers”

  3. Hi! Very nice app! ;)

    BUT – You would be my herror if you could make the text chat working at a Wowza server as well!!

    Can you do that? All you have to do is to “convert” is the server side as file to java.

    Best regards,
    Pablo

  4. I am not sure if you can convert to Wowza server. It seems possible since Wowza is an alternative to Flash Media Server.

  5. Omigod! Thanks for the info about the differences in AMF. You just saved me a very miserable night trying to get my Flex app to talk to my FMS.

    Cheers!!

  6. Hi again!
    What would it take to make you convert it to wowza? I really need it! I’m willing to pay you if you can make it…!

    ps. Is it absolutely nessesary to use server side code? I have seen an example made in flash and as2 with no server side code, which also used the shared object! Because…if it does not use server side code, it would work on wowza as well.

    I really hope you can help me!

  7. Yeah, don’t have the time dude. You can just keep a database for the information right? Just keep track of users and handle it in the client.

  8. Thanks for the code! I was pulling my hair out on getting remote objects to work. Your code saved the day.

  9. i am new to fms2 and i am building a simple chat and id like to know can i 2 users send a private message to each other how is there any examples or tutorials i can be inspiring thank you

  10. Hi mister!

    I’m new to flex and all this stuff (I’m a java dev) and I have to develop a chat using flex and openfire, do you know where I can find some examples or good documentation?

    thanks

  11. Brilliant. I was up all night trying to get this working. Is going now, thanks :), but I still dont understand why I cant just connect to the sharedobject from my client and set a value like
    so.data.thing = “somedata”;
    and that update everything else. It works in Flash 9 CS3 but not Flex 2 or 3 beta 2 even with the AMF setting set to 0.

  12. You should be able to set the shared object in the client. Maybe you should use flush:

    var so:SharedObject = SharedObject.getLocal( “myvar”);
    so.data["myvar"] = false;
    so.flush();

  13. Yes thanks for the info, sorry I wasnt very clear, when I said “connect to the sharedobject from my client”, I meant connect to the remote shared object from the client.

    The way it works in flash is that you dont need to write any code on the server. Just call the

    my_so:SharedObject = SharedObject.getRemote(“my_so”) and set the data like this
    my_so.data.value = “a value”;

    this should then automatically get sent to the server, and any other clients connected to the server should get their shared object sync events fired?

    But the way you do it is by creating the so on the server then using method calls from the client to update it, then using another message from the server to pass back out to the clients. Which is great that it works, but just not how any of the documentatino that ive read shows how to do it.

    ps, ive come from a .Net background so flash and AS is pretty new to me.

  14. hey

    Is there any way by which the shared objects can be destroyed as soon as the browser is closed????

  15. I wanted to use the code with Openfire server, but is not working, what should I do to configure Openfire or the chat client? I’m new to this stuff…

    Thanks

  16. Nice post. may i know about the chat_test. I have been trying to develop a chat application in flex but got stuck in the middle. can anyone help, chat_test is not making much of sense to me, what is actually there in it?

  17. Hi,
    I have gone thru this chat app it’s really very good, I just have 1 querry that when I use following coding:
    nc.connect(“rtmp://kurosawa.wpcareyonline.com/chat_test”, userName.text);
    then all things go fine but if I have installed the folder “chat_test” at root of my web and try this coding:
    nc.connect(“rtmp://ewaresol.com/chat_test”, userName.text);
    then it does’nt connects please tell me that where am I doing mistake, Pls do reply I am getting very much upset on failure
    Thanks a Lot
    Arsalan Ahmed

  18. Can you brief how to that message to be send only between two clients? (Creating private chat)
    With your example message are send to all clients that is connected to the shared object “ChatUsers”

  19. Hello
    Nice application but when we have type the message and press enter its does not working agian i have press send button. if i need after typing the message and i press enter the message will be send could u tell me the procedure wat should i do to work enter button plz sir. help me

  20. Hello
    First let me congratulate for designing a nice application.
    But i have a small doubt..
    When i enter a message in the text box and press the Enter button the message is not getting displayed in the chat window.
    How to make the enter button work?
    Thanks in advance.
    Razzak

  21. Pingback: Bookmark: 60+ Adobe Flex reference websites

  22. Yes I got what i was searching from last 4 days. Thanks alot for this. If you have some more work or project or information about socket programming flex than let me know please.

    Thanks once again……….

  23. hi…
    Thanks for the such a nice application? when i saw the code then there was a line of code
    “nc.connect(“rtmp://kurosawa.wpcareyonline.com/chat_test”, userName.text); ” that was working fine. but when i connect the root of my server then it was not working. What is the problem? what should i do to connect it.

    Thanks……….

  24. hi Mister and thanks for this application !!!

    i’ve installed FMS 3.5 and Flex Builer3 ,

    i created a directory called “chat_test” in “/applications ”

    when i run the application and click connect ( after putting a name)

    the name i sent is not displayed neither the messages i put in screen ..

    could you help me please so i can run it properly

  25. i got this trace

    netStatus: [NetStatusEvent type="netStatus" bubbles=false cancelable=false eventPhase=2 info=[object Object]]
    code : NetConnection.Connect.Success
    description : Connection succeeded.
    objectEncoding : 3
    level : status
    data :

    • @wajdi, Not sure why, but you don’t seem to be passing any data object, you need to trace out before the call that you are indeed passing data.

  26. Hi,
    I have gone thru this chat app it’s really very good, I just have 1 querry that when I use following coding:
    nc.connect(”rtmp://kurosawa.wpcareyonline.com/chat_test”, userName.text);
    then all things go fine but if I have installed the folder “chat_test” at root of my web and try this coding:
    nc.connect(”rtmp://netchargers.com/chat_test”, userName.text);
    then it does’nt connects please tell me that where am I doing mistake, Pls do reply I am getting very much upset on failure
    i got this trace

    netStatus: [NetStatusEvent type="netStatus" bubbles=false cancelable=false eventPhase=2 info=[object Object]]
    code : NetConnection.Connect.Success
    description : Connection succeeded.
    objectEncoding : 3
    level : status
    data :
    Thanks a Lot

  27. Iam unable to get the connection object of the server when i use applet id.connect() method, can any one help?? thanks in advane

    • Only if there is some kind of on closing event, which you can detect and then destroy the shared objects. But really, what good is having a shared object if its only temporary, you can just install data temporarily within your own application, no need for a shared object.

  28. Pingback: 12 Very Useful Applications Based on Flex/Flash - Ntt.cc

  29. Pingback: ??-IT Player » Blog Archive » ??60+ Flex??????

  30. This is perfect, thanks mister,

    But when I tested the application on FMS 3.0.1, I get the following error:

    Connection rejected by server. Reason : [ Server.Reject ] : (_defaultRoot_, _defaultVHost_) : Application (liveChat/_definst_) is not a valid signed application; loading access denied.

    I have tried to make it a “vod” application and tried also “live” application, both; when I rename or delete main.far and place main.asc I get this error!

    Any ideas?

  31. hi,
    i need class java.
    the code works only on that specific address. like java class to understand the operation. would be a huge help to me.
    thanks for the help.

    change
    -> nc.connect(”rtmp://kurosawa.wpcareyonline.com/chat_test”, userName.text);
    for
    -> nc.connect(“rtmp://localhost/chat_test”, userName.text);

  32. Hi, this source is great. My problem is working out how to filter out specific users by a stored array of friends on a database. I can filter the default users list to show only these friends however selecting one of these users from the list and filtering out chat data from everyone, apart from the selected friend is another story.

    What would be the best way to filter this chat content?

  33. I worked with this chat application in a local system as well as in LAN. It works fine,if flex is installed Flex in every PCs with only one FMS installed in one system.But i use flazeds j2ee server then it gives results with local system, but i connect via LAN it produces net connection failure.Please suggest useful idea to overcome this failure

  34. Only if there is some kind of on closing event, which you can detect and then destroy the shared objects. But really, what good is having a shared object if its only temporary, you can just install data temporarily within your own application, no need for a shared object. sohbet odalar? -

  35. I worked with this chat application in a local system as well as in LAN. It works fine,if flex is installed Flex in every PCs with only one FMS installed in one system.But i use flazeds j2ee server then it gives results with local system, but i connect via LAN it produces net connection failure.Please suggest useful idea to overcome this failure

  36. Nice project , i am new to flex can u say how to do this project for communicating with single client here the mess is displaying to all i need to display to only one usr

  37. Pingback: FMS, Cairngorm, and Flex « Thanks, Mister!

  38. Hi, i am trying to run your code but it show error. Could you please guide me to resolve this issue.

    Initializing application… in player: WIN 10,1,102,64

    netStatus: [NetStatusEvent type="netStatus" bubbles=false cancelable=false eventPhase=2 info=[object Object]]
    description : Connection succeeded
    data : [object Object]
    code : NetConnection.Connect.Success
    objectEncoding : 0
    level : status

    Connecting non-persistent Remote SharedObject…

    netStatus: [NetStatusEvent type="netStatus" bubbles=false cancelable=false eventPhase=2 info=[object Object]]
    code : NetConnection.Connect.Closed
    level : status

    Sorry your connection closed
    Reason is: undefined

Comments are closed.