Building Collaborative Applications with Flex Data Services and Flash Media Server

I presented a session called “Building Collaborative Applications with Flex Data Services and Flash Media Server” at MAX (the Adobe developer conference) last week. Beyond the basics of the Flex pub/sub messaging infrastructure, the session provided techniques to build real life collaborative applications. I started with a simple chatroom, and then incrementally added features to end up with an IM client supporting presence and videoconferencing.

Many people have asked me to make my code available. So here it is. If you didn’t attend MAX, I added a brief description for each version of the application.

To run these samples, you need to install the Flex Data Services. If you haven’t already done so, you can download the Flex Data Services here, and follow the installation instructions.

Install the samples
  1. Download flex-collaboration.zip here and expand it on your local file system
  2. Copy the flexchat directory in the context root of your web application
  3. Copy the WEB-INF\src and WEB-INF\classes directories in the corresponding directory structure of your web application
Install the flexchat database

Starting with chat3.mxml, you will need a MySQL database.

  1. Create a new MySQL database or use an existing one
  2. Import the flexchat database using the flexchat.sql script
  3. Make sure a MySQL JDBC driver is available to your web application
  4. Edit flexchat.properties in WEB-INF\classes, and make sure the connection properties match your configuration
Install Flash Media Server

chat6.mxml requires Flash Media Server to enable videoconferencing.

  1. Install the Flash Media Server: you can download a free developer edition here (click free developer edition)
  2. In C:\Program Files\Macromedia\Flash Media Server 2\applications, create a directory called flex_videoconference and copy the main.asc file in that directory
Samples Walkthrough
chat1.mxml

chat1.mxml is a minimal chat application showing how to use the pub/sub messaging capabilities of Flex with just a couple of lines of code.

In this version, messages posted to the “chat” destination go to all the clients who subscribed to it. This is appropriate for a multi-user chatroom use case, but not for instant messaging applications where you want private conversations with individual users.

To run this sample, define a “chat” destination in messaging-config.xml as follows:

    <destination id="chat">

        <properties>
            <network>
                <session-timeout>0</session-timeout>
            </network>
            <server>
                <max-cache-size>1000</max-cache-size>
                <message-time-to-live>0</message-time-to-live>
                <durable>false</durable>
            </server>
        </properties>

        <channels>
            <channel ref="my-rtmp"/>
        </channels>

    </destination>

You may need to restart your server after modifying messaging-config.xml.

chat2.mxml

In this version, we use subtopics to enable private conversations between users. If you want to send a message to a user named Joe, you publish your message to the “Joe” subtopic of the chat destination (chat.Joe). Similarly, if Joe only needs to receive the messages that are sent to him, his client application can subscribe to the “Joe” subtopic.

Subtopics don’t need to be declared. All you need to do is configure your destination to support subtopics as shown below (allow-subtopics and subtopic-separator tags):

    <destination id="chat">

        <properties>
            <network>
                <session-timeout>0</session-timeout>
            </network>
            <server>
                <max-cache-size>1000</max-cache-size>
                <message-time-to-live>0</message-time-to-live>
                <durable>false</durable>
                <allow-subtopics>true</allow-subtopics>
                <subtopic-separator>.</subtopic-separator>
            </server>
        </properties>

        <channels>
            <channel ref="my-rtmp"/>
        </channels>

    </destination>


chat3.mxml

In this version, we add a buddy list using a DataService. The buddy list is displayed after the user logs on. You need to install the MySQL database to run this application.

To run this sample, add a “chat-buddies” destination in data-management-config.xml as follows:

    <destination id="chat-buddies">

        <adapter ref="java-dao" />

        <properties>
            <source>flex.samples.chat.BuddyAssembler</source>
	    <scope>application</scope>

            <metadata>
                <identity property="userId"/>
                <identity property="buddyId"/>
            </metadata>

            <network>
                <session-timeout>20</session-timeout>
                <paging enabled="false" pageSize="10" />
                <throttle-inbound policy="ERROR" max-frequency="500"/>
                <throttle-outbound policy="REPLACE" max-frequency="500"/>
            </network>
        </properties>

    </destination>


chat4.mxml

In chat4.mxml, we add a custom adapter (ChatAdapter.java) to the chat destination to support basic presence information. ChatAdapter implements the FlexSessionListener interface and uses the sessionCreated and sessionDestroyed callback methods to keep track of when users connect and disconnect, and notify interested parties.

The client application uses separate consumers to subscribe to a “status” subtopic for each buddy in the buddy list (chat.status.jane, chat.status.eric, chat.status.susan, etc.). See the dsBuddiesResult() method for the implementation details.

To run this sample, add our simple ChatAdapter in the list of configured adapters in messaging-config.xml. Th list of configured adapters should look like this:

    <adapters>
        <adapter-definition id="actionscript" class="flex.messaging.services.messaging.adapters.ActionScriptAdapter" default="true" />
        <adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter"/>
        <adapter-definition id="chat" class="flex.samples.chat.ChatAdapter"/>
    </adapters>

Next, specify that you want to use the chat adapter in the chat destination by adding the adapter reference as follows:

    <destination id="chat">

        <properties>
            <network>
                <session-timeout>0</session-timeout>
            </network>
            <server>
                <max-cache-size>1000</max-cache-size>
                <message-time-to-live>0</message-time-to-live>
                <durable>false</durable>
                <allow-subtopics>true</allow-subtopics>
                <subtopic-separator>.</subtopic-separator>
            </server>
        </properties>

        <channels>
            <channel ref="my-rtmp"/>
        </channels>

        <adapter ref="chat"/>

    </destination>

The unresolved issue in this version is that when you log on, you still don’t know which of your buddies are *already* online, and what their status is.

chat5.mxml

In this version, we use a DataService (“chat-sessions”) to keep track of the active user sessions along with presence information. Just like we did in ChatAdaper, the UserSessionAssembler class implements the FlexSessionListener interface and uses the sessionCreated and sessionDestroyed callback methods to create and remove user sessions. Using a DataService to keep track of presence information is convenient because changes (i.e. status information) are automatically pushed to all interested clients.

To run this sample, add a “chat-sessions” destination in data-management-config.xml as follows:

    <destination id="chat-sessions">

        <adapter ref="java-dao" />

        <properties>
            <source>flex.samples.chat.UserSessionAssembler</source>
	        <scope>application</scope>

            <metadata>
                <identity property="userId"/>
            </metadata>

            <network>
                <session-timeout>20</session-timeout>
                <paging enabled="false" pageSize="10" />
                <throttle-inbound policy="ERROR" max-frequency="500"/>
                <throttle-outbound policy="REPLACE" max-frequency="500"/>
            </network>

        </properties>

    </destination>


chat6.mxml

This version is the same as chat5.mxml with the addition of videoconferencing features enabled by Flash Media Server. Start your webcam in one session. in the buddy list of another session, click the name of the user who is broadcasting to see the video feed.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • LinkedIn
  • StumbleUpon
  • Twitter
This entry was posted in Collaboration, Flash Media Server, Flex. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

93 Comments

  1. kurt
    Posted September 22, 2007 at 7:08 pm | Permalink

    Thanks for your great tutorial!!

  2. Posted October 7, 2007 at 9:37 pm | Permalink

    Those of you who are getting the error “Destination ‘chat-buddies’ must specify at least one channel.” on chat3.mxml just need to add:

    … after the tag but before the tag in data-management-config.xml

  3. Robert
    Posted October 24, 2007 at 2:11 am | Permalink

    i am getting these “you must specify at least one channel”, when starting jrun. what do i need to add in the data-management-config.xml?

  4. Posted October 24, 2007 at 10:25 am | Permalink

    I guess the tags I wrote in my previous post from October 7th didn’t show up. I’ll leave out the angle brackets and hopefully then they will show up. The part you need to add is the

    channels
    channel ref=”my-rtmp”/
    /channels

    before the final destination tag.

  5. shiv
    Posted November 6, 2007 at 12:15 am | Permalink

    i am pretty new to adobe products… any idea how to make the IM client….

  6. shiv
    Posted November 6, 2007 at 11:03 pm | Permalink

    [MessagingError message='Destination 'chat-sessions' has no channels defined and the application does not define any default channels.']
    at mx.messaging.config::ServerConfig$/::internalGetChannelSet()
    at mx.messaging.config::ServerConfig$/getChannelSet()
    at mx.messaging::MessageAgent/mx.messaging:MessageAgent::initChannelSet()
    at mx.messaging::MessageAgent/mx.messaging:MessageAgent::internalSend()
    at mx.messaging::Producer/mx.messaging:Producer::internalSend()
    at mx.messaging::AbstractProducer/connect()
    at mx.data::DataStore/http://www.adobe.com/2006/flex/mx/internal::initialize()
    at mx.data::ConcreteDataService/createItem()
    at mx.data.mxml::DataService/createItem()
    at chat6/::logon()
    at chat6/___Button1_click()

  7. Geo
    Posted December 4, 2007 at 3:04 am | Permalink

    HI, very nice site with a lot of useful information concerning AIR, Flex and FCS(2).

    I would like you tell us what do you think about the open source red5 (see http://osflash.org/red5) instead of proprietary Adobe’s Flash Communication Server 2 (FCS2). Have you experience with red5, what is it all about – is scalable, how many transactions can handle, etc?

    Regards
    man

  8. Posted December 4, 2007 at 6:34 pm | Permalink

    What is the Dejа vu? What is this fleeting imprinted in the memory?

    P.S. Please administrator coenraets.org. If the thread is not to be in category этот, I ask you to move my thread to the correct category.

  9. junior
    Posted December 18, 2007 at 10:28 am | Permalink

    Hello, I try the chat1.mxml and I get the follow error:

    VerifyError: Error #1053: Sustitución no válida de subtopic en mx.messaging.Consumer.

    at flash.display::MovieClip/nextFrame()
    at mx.managers::SystemManager/deferredNextFrame()
    at mx.managers::SystemManager/preloader_initProgressHandler()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.preloaders::Preloader/timerHandler()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()

    Can you hel me please?

  10. Posted February 11, 2008 at 10:07 am | Permalink

    great post.thanx.

  11. Vivek
    Posted February 12, 2008 at 5:36 am | Permalink

    can tell me how to use H264 codec to develop a video chat application in flex & FMS 3

  12. Yamuna
    Posted February 15, 2008 at 8:26 am | Permalink

    I didn’t work this program.Can you plese tell how to run this program..?i put the source files in lcds folder but i didn’t get the out put…please tell me the solution for that….

  13. Claudiu
    Posted March 24, 2008 at 9:03 am | Permalink

    I am curious up to how many users can this chat take?

  14. Posted April 1, 2008 at 3:28 am | Permalink

    Thank you! It’s really interesting and information is very helpful! Cool!

  15. Student
    Posted April 9, 2008 at 3:59 am | Permalink

    When I tried to compile the Java files BuddyAssembler I got an error Class not found. All the other files are compiles. I think is it is due to the package flex.data.assembler.AbstractAssembler

    What should I do then? Also Abstract class is a build in class in flex right? Then why this error is occuring? When I went to this path I haven’t find any such class. What should I do?

    Please help me…

  16. Shiv Satchit
    Posted April 30, 2008 at 7:15 am | Permalink

    I am not leaving a reply and I hope you don’t mind. I have gained quite a lot reading the questions and answers.

    I wss thinking about incorporating a multi-functional online chat utility into our charity website but not as good as this one made in Flex and powered by FDS.

    I am not a programmer, just an elementary web builder. I was wondering if there was someone out there in the cyberspace who would be kind enough to spare some time to help us embed the coding in our site. He or she could either do it for us or perhaps guid us through. Thanks a lot. Please help us as it is for humanitarian work – promoting socio-economic development in the least developed countries. my contact number is 00919810036363 as I am currently working in India trying to learn Flex in order to weave an RIA web application.

  17. Mike
    Posted April 30, 2008 at 2:44 pm | Permalink

    I am trying to compile the chat adapter (mostly because I wanted to repackage it). I am getting an error with the line:

    MessageService msgService = (MessageService) service;

    Error message is “service cannot be resolved”

    I have all the packages reference for the import statements. Any idea what is causing this error?

  18. Posted May 6, 2008 at 10:13 am | Permalink

    @Mike you need to replace service with getDestination().getService().

    Adrian.

  19. nana
    Posted May 25, 2008 at 1:48 am | Permalink

    Since I am completely to all this, I was hoping that you might have a dummy proof version of a step by step tutorial. I have installed Flex and Flesh Media Server. Since I have been only using Wamp, I am a little confused where to extract flex-collaboration.zip files since I couldn’t find WEB-INF. I would greatly appreciate any help.

  20. Santosh
    Posted June 5, 2008 at 7:18 am | Permalink

    how can i run this application with tomcat 5.5 please help me.

  21. Santosh
    Posted June 5, 2008 at 10:44 pm | Permalink

    [MessagingError message='Destination 'chat' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']
    at mx.messaging.config::ServerConfig$/internalGetChannelSet()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\messaging\config\ServerConfig.as:624]
    at mx.messaging.config::ServerConfig$/getChannelSet()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\messaging\config\ServerConfig.as:255]
    at mx.messaging::MessageAgent/initChannelSet()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\messaging\MessageAgent.as:1083]
    at mx.messaging::MessageAgent/internalSend()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\messaging\MessageAgent.as:1059]
    at mx.messaging::Consumer/internalSend()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\messaging\Consumer.as:224]
    at mx.messaging::AbstractConsumer/subscribe()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\messaging\AbstractConsumer.as:624]
    at chat1/logon()[E:\FlexProject\ChatApplication\FMSChat\src\chat1.mxml:12]
    at chat1/___chat1_Button1_click()[E:\FlexProject\ChatApplication\FMSChat\src\chat1.mxml:42]

    when i run chat1.mxml it gives above error please advise me where i copy messaging-config.xml file

  22. Santosh
    Posted June 20, 2008 at 4:31 am | Permalink

    how can i run this application with tomcat 5.5 please help me..

  23. Gianluca Bruno
    Posted June 21, 2008 at 9:35 pm | Permalink

    I can’t resolve the AbstractAssembler and DataServiceTransaction classes. What do I use in replacement of these?

  24. Jake
    Posted July 16, 2008 at 11:51 am | Permalink

    Great tutorial. I have been building my own Instant messenger with Flex Data Service but am struggling to see how to add a buddy where it sends a request for the recipient to accept this. I can only see how to do this with XMPP. Does flex data services lack this functionality? If anyone knows could you leave a quote on where to find some documentation on this or an example.

  25. Jake
    Posted July 16, 2008 at 12:01 pm | Permalink

    Does anyone know in an instant messeneger how to. If i want to view cam with another user online to send a request for the user to accept this so there is some kind of security instead of just plain streaming.

  26. Posted August 2, 2008 at 9:37 am | Permalink

    Hello, coenraets.org
    virtually a year ago now, my computer got a indeed genuine virus. I’m not infallible how it started, but my computer kept infuriating to run unrecognized programs and would and frameset problems refrigerate up eDialect rightage it was turned on from vexing to run them. My cousin who has IT episode tried to boost us out. We got the computer stabilised and workable and ended up re-installing Windows more than the corrupted version. At the after we ratiocination the enigma was dealt with. yet, we installed multiple virus scanners and ran scans with AVG unshackle, Virusinspect, Spybot, A
    d-au courant, Xoft-Spy, Stinger and an Online inspect from Symantec safeguarding Check. Most of these were pronouncement no puzzlers, but AVG detected divers viruses that it couldn’t mend and filed away. The computer was marginally slower, but wasn’t too noticable at the after . across the aftermost few months, the computer has been getting slower and slower and a stupendous amount of array on the pragmatic allude has disappeared. We had connected with 100GBs sinistral after the virus and now are down to up 10GB regular even so we are constantly un-installing programs and we haven’t satisfactorily files or programs on the computer to good up all the mystified blank. This combined with the computer slowing down to a snail-grovel precipitousness all the after has made it frustrating to use at best. If anyone could swear off me any assistants, it would be mere much appreciated.
    Deckard’s structure inspect
    Run by Gerry on 2008-06-23 01:10:41
    Computer is in sane Mode.

  27. Posted August 22, 2008 at 5:15 pm | Permalink

    Great application!

  28. Posted August 22, 2008 at 5:15 pm | Permalink

    Great application thanks;)

  29. Posted August 22, 2008 at 5:16 pm | Permalink

    güzel bilgi teşekkürler.

  30. Posted September 17, 2008 at 2:17 am | Permalink

    I can’t resolve the AbstractAssembler and DataServiceTransaction classes. What do I use in replacement of these?

  31. Posted November 20, 2008 at 9:16 pm | Permalink

    [...] TileList Control :: Dragging Items From a TileList Control To A Container Christophe Coenraets :: Building Collaborative Applications with Flex Data Services and Flash Media Server Daniel Wanja :: Flex introspection API: describeType(value:*):XML :: Find a class’s [...]

  32. Posted February 23, 2009 at 4:12 pm | Permalink

    I use in replacement of these?

  33. Posted May 2, 2009 at 7:11 pm | Permalink

    Great application thanks;)

  34. Posted May 16, 2009 at 2:23 pm | Permalink

    Thank You Admin Amca mCx :)

  35. Posted May 16, 2009 at 2:24 pm | Permalink

    Thanks

  36. sohbet
    Posted May 17, 2009 at 9:01 am | Permalink

    hallo i wish you verry succes operator

  37. Naresh
    Posted May 24, 2009 at 1:26 am | Permalink

    its good,
    Iam new to messaging.
    Please provide the messaging-config.xml
    Because it is not going show properly

  38. Posted July 2, 2009 at 2:22 am | Permalink

    excellent

  39. Posted July 13, 2009 at 7:13 am | Permalink

    I didn’t work this program.Can you plese tell how to run this program..?i put the source files in lcds folder but i didn’t get the out put…please tell me the solution for that….

  40. Posted September 28, 2009 at 11:30 am | Permalink

    This is very nice! You make a very good job. Thank for the information!

  41. Posted November 24, 2009 at 6:32 am | Permalink

    how are you Babyyy

  42. Posted November 24, 2009 at 6:33 am | Permalink

    how are you baby nina

  43. Bala
    Posted December 1, 2009 at 8:18 am | Permalink

    When i setup the project and execute , i am getting the following error.

    “Cannot create class of type ‘flex.samples.chat.UserSession’.”

    Kindly help me in fixing this.

11 Trackbacks

  1. By thefactoryfactory on October 31, 2006 at 8:30 am

    [...] That was a fantastic lecture by the way, and it’s awesome to see the code up. It’s all here [...]

  2. [...] If you’re looking for an example application (with code) to help you get a better handle on how Flex2 can leverage Flash Media Server and LiveCycle Data Services, then you’ll find Christopher Coenraets article (named link) a very enlightening read Building Collaborative Applications with Flex Data Services and Flash Media Server  . [...]

  3. By Flex Coding « Panduramesh’s Weblog on September 19, 2008 at 7:21 am

    [...] TileList Control :: Dragging Items From a TileList Control To A Container Christophe Coenraets :: Building Collaborative Applications with Flex Data Services and Flash Media Server Daniel Wanja :: Flex introspection API: describeType(value:*):XML :: Find a class’s [...]

  4. By About LCDS « It’s all about RIA on October 14, 2008 at 12:26 am

    [...] Christophe Coenraets :: Building Collaborative Applications with Flex Data Services and Flash Media Server [...]

  5. By It’s all about RIA on October 14, 2008 at 12:44 am

    [...] Comment! Building Collaborative Applications with Flex Data Services and Flash Media Server [...]

  6. By Adobe LCDS « Flex Generation Weblog on October 14, 2008 at 3:12 am

    [...] Christophe Coenraets :: Building Collaborative Applications with Flex Data Services and Flash Media Server [...]

  7. By FLEX CODING « welcome nandhu on October 22, 2008 at 10:53 pm

    [...] Christophe Coenraets :: Building Collaborative Applications with Flex Data Services and Flash Media Server [...]

  8. By Flex Coding « Rameshgoud’s Flex Weblog on October 25, 2008 at 6:15 am

    [...] TileList Control :: Dragging Items From a TileList Control To A Container Christophe Coenraets :: Building Collaborative Applications with Flex Data Services and Flash Media Server Daniel Wanja :: Flex introspection API: describeType(value:*):XML :: Find a class’s [...]

  9. By Flex Coding « Adiflex’s Blog on October 25, 2008 at 6:15 am

    [...] TileList Control :: Dragging Items From a TileList Control To A Container Christophe Coenraets :: Building Collaborative Applications with Flex Data Services and Flash Media Server Daniel Wanja :: Flex introspection API: describeType(value:*):XML :: Find a class’s [...]

  10. By Adobe LCDS « SrikanthCreative Mind’s Weblog on October 27, 2008 at 6:59 am

    [...] Christophe Coenraets :: Building Collaborative Applications with Flex Data Services and Flash Media Server [...]

  11. By dashboards « Adiflex’s Blog on October 29, 2008 at 9:38 am

    [...] Christophe Coenraets :: Building Collaborative Applications with Flex Data Services and Flash Media Server [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>