Flex 2 and Red 5 Chat Example

Posted on October 13, 2006 | 30 comments

NOTE: This is old code and will probably not work since Red 5 has progressed. The Flex 2 code is still a good basis of how to use NetConnection and NetStream.

I have created a basic chat example using Flex 2 and Red 5. The purpose of this example is not so much about building a Red 5 server application but getting Flex 2 to work with an existing Red 5 sample server application.

All you’ll need to do is install Red 5 and make sure the samples work. You can find more information on Red 5 at http://osflash.org/red5, If you do use a non default install of Red 5 you’ll need to change the NetConnection connection URL from rtmp://localhost/fitcDemo to the appropriate value.

This basic chat example demonstrates how to force Flex 2 to use AMF0 and get around the Flex 2 NetConnection and SharedObject not being dynamic. Hopefully the example is basic enough to not require a big long walk through.

You can get the example here.

The live example is located at http://renaun.com/flex2/red5/BasicChat.html

  • http://www.igorcosta.com Igor Costa

    Good sample man.

  • http://www.me.com/patrickdj Patrick

    Only one time out of about a hundred did i see any of the shared objects net work correctly with Red5. What’s weird is the connection is made, and if i open my other fitcDemo flash chat control, it displays all of the information working inside flex.

    When I log in i don’t even see my name added to the list.

    Using – Flex 2 and .6rc1 of red5

  • http://www.renaun.com Renaun Erickson

    I think I remember some weird side effects while having the Flex 2 client and the Red5 Flash client open at the same time. Not sure what is causing the issue. You need to close down the browsers and restart Red5 and only us one of them.

    It would be better to copy over the sample app and make it specific for Flex 2 clients.

  • http://www.me.com/patrickdj Patrick

    Renaun,

    I’m still having a weird issue, the list or the actual chat messages aren’t returning into flex. I’m not connecting through localhost but through our server. If you have a moment open both these links at the same time please:

    http://64.124.52.252/chat/BasicChat.swf
    http://64.124.52.252/red5/root/demos/videoConference.swf

    Also, if you want to connect directly to our red5 server – you can use this link:

    http://64.124.52.252/red5/fitcDemo

  • http://www.me.com/patrickdj Patrick

    Actually our server is for the fitcDemo is:

    http://64.124.52.252/fitcDemo

  • http://www.renaun.com Renaun Erickson

    I could never get the two to work on my local machine correctly. Since I was trying to create an example I didn’t spend any time trying to figure out why Red5 had issues. Its really a question for the mailing list.

    I tried your two links and couldn’t either to connect.

    Close all the Flash Red5 examples and restart the Red5 server, and then only use the Flex client does it work correctly?

  • http://www.me.com/patrickdj Patrick

    Quick question which version or red5 did you use?

  • http://www.renaun.com Renaun Erickson

    0.5rc1

  • Uwe

    Hi I,
    downloaded your source files and it don´t work.
    I userlist and chat-messages are not displayed.

    When I press the loginbutton the Server Console prints an Error:
    [ERROR] 237359 pool-1-thread-5:( org.red5.server.net.rtmp.RTMPHandler.error ) Exception
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1

    I hope you can help me,

    Uwe

  • http://www.renaun.com Renaun Erickson

    What version of Red5 are you using, this code has not been updated to 0.6

  • Uwe

    thank you for the fast reply…

    I´m using 0.6
    is this the problem ?

    are there so many differences between 0.5 and 0.6 ?

  • http://www.renaun.com Renaun Erickson

    yes there are a lot of differences, I haven’t had the time to update the code yet. I believe there is another Flex 2 example for 0.6 release done by John Grden

  • Uwe

    Joachim Bauch wrote:
    > Hi,
    >
    > やすえ けん schrieb:
    >> Hi all, I got this exception in creating new shared object.
    >> I used Red50.6rc1 in Eclipse3.2 in WindowsXPSP2.
    >>
    >> [ERROR] 334938 pool-1-thread-4:(
    >> org.red5.server.net.rtmp.RTMPHandler.error ) Exception
    >> java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    >
    > please try r1533 on the trunk to see if this fixes your problem.
    >
    > Joachim

    maybe this solve the problem

  • yarek

    This is a wonderful sample.
    But it seems it has some latency:

    it seems that shared objects do not synchronyse at when a client leaves the
    chat:
    http://64.124.52.252/chat/BasicChat.swf

    open an instance
    open an second instance of the chat
    close the first instance: the user is still there !

    it does refresh users only at the connection. I have tried with AS2.0 : the
    onSync works perfectly but does not works in Flex in AS3.0
    Is this a bug of Flash 9 ?

    Do you have any solution for this ?
    Thanks again for your great POST !

  • Aaron Bell

    Hi,

    I ahve Red 5 installed and can get their chat demo going (though only in firefix for some reason). Your live sxample above does nto seem to do anything. It looks great but doesd not add you to the list or anything. What might I be doing wrong please?

  • http://www.jeffbaier.com Jeff Baier

    Thanks for this great example! I am new to Flex, but I was able to integrate your example into a project and recompile it. Its working successfully on a remote server. However, I have noticed that once you have a certain amount of chat messages, the chat window does not scroll automattically with new messages. I was wondering how this could be accomplished? Or if there is some documentation or tutorials you could point me to? I’m trying to build out more features in this simple chat app as a way to learn more about Flex.

    Thanks again.

  • http://www.renaun.com Renaun Erickson

    @aaron The red5 server must be down on my server. Not sure if i’ll bring it back up any time soon.
    @jeff The code:
    updateComplete=”txtChatBox.verticalScrollPosition = txtChatBox.maxVerticalScrollPosition + 1;”
    Should be what you are looking for, its in the source.

  • http://www.jeffbaier.com Jeff Baier

    I didn’t see that anywhere in the source, but I added that to the mx:TextArea and it works great now. Thanks.

  • sumant

    hi

    I read ur comments. Now I can create chat application using which I can connect and chat on my local machine. But I am not able to create a login page and create chat rooms. And also I don’t know how to upload my files in the server because I don’t know the server will support the Red5 or not.

    Can you please say me, how can I create chat rooms and user login pages. After login the user can be able to chat individually with separate rooms.

    Please suggest me or if there is any article which will guide me to create a complete chat application as yahoo chat.

    Thanks

    Sumant

  • http://www.red5chat.com yarek

    here comes another open source Flash chat in RED5

  • abhishek

    here is showing error while running the live demo :
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at BasicChat/sendMessage()
    at BasicChat/__btnMessage_click()

  • marius

    I use this chat for a small app and it’s great.
    I have a problem : when I update the shared object on the serverside, the clients connected do not receive notifications in the SyncEvent.SYNC handler.

    so.beginUpdate();
    so.setAttribute(“testAttribute”, “testValue);
    so.removeAttribute(“Three”);
    so.endUpdate();
    so.notifyAll();

    PS: if I edit the shared object’s data content on the client side, I receive notifications on all users.

    so.data.testClient = “testValue”; so.setDirty(“testClient”);

  • marius

    solved. should’ve taken the scope each time, before setting the attributes.

  • http://sunil-gupta.blogspot.com Sunil Gupta

    Nice example of chat with Red5.

  • Michael

    Any help on this will be greatly appricated, I build a chat using Flex and Red5, the text chat does not scroll to show the most recent chat, do you have any idea of a fix in Flex.
    I am not sure if the solution on this forum work?
    help need, thank you..

  • http://www.renaun.com Renaun Erickson

    did you try using this code in the TextArea (ie: TextArea with id=txtChatBox)?

    updateComplete=”txtChatBox.verticalScrollPosition = txtChatBox.maxVerticalScrollPosition + 1;”

  • Chat Checker

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at BasicChat/sendMessage()
    at BasicChat/__btnMessage_click()

  • http://red5flashserver.blogspot.com Red5FlashServer

    Thanks for sharing nice example.

  • Michael

    Nice work, but in my Flex builder compiler show 1120: Access of undefined property Loger, please help me

  • http://www.renaun.com Renaun Erickson

    The Logger class is the RIALogger component that you can find on my site. The Logger class has changed but you can just comment out the statements in this code and try to compile it.