Basic SharedObject example in Flex, Red5 & Flash Media Server

By robsilverton

Prerequisites:

• Intermediate AS3 knowledge
• Flex 3
• Flash Media Server and/or Red5 + Java EE

Resources:

View Flex source
View Flash Media Server Source
View Red5 source

Download Flex source
Download Flash Media Server Source
Download Red5 source

An indispensable guide to migrating from FMS to Red5 by Joachim Bauch

Includes:

• A Flex Project that can connect to either the Red5 or FMS back end, depending which app/server you are running on localhost.
• A simple FMS SharedObject demo .asc file.
• The equivalent Red5 SharedObject demo Java Project.

Tips:

Getting errors in your Java project? Try right clicking on the Java project and going to “Properties/Java Build Path”. Remove any references to unbound libraries and click “Add External JARs…”, then navigate to red5.jar in the root of your Red5 installation.

Can’t select to “Debug As/Debug on Server” in your Java project? Right click on your Java project and go to “Properties/Project Facets”. Make sure to select “Dynamic Web Module” and “Java”, then apply the changes.

 

The purpose of this example is to compare on a basic level the experience of developing for Red5 and FMS.
The application allows users to send messages to all users who have the application open in real time.
The great thing about the way Red5 is implemented is that from the Flex client’s perspective you are dealing with it in exactly the same way you would deal with FMS, so if you need to port a bunch of your existing FMS code over to Red5, you may well not need to change your Flex client code at all.

All the FMS and Red5 versions of the back end are doing in this example is the following:

• on receiving a client connection they create a non persistent server side SharedObject named “message” as a means of broadcasting messages to clients.
• next they define a “sendMessage” function for the Flex client to call when the “Send” button is pressed. This function accepts a message string, which is then passed back to all connected clients via the “message” SharedObject calling the “receiveMessage” function defined in the Flex client code.

Differences in implementation:

In FMS the application entry point is implicitly created for you and is available as an object named “application”. As an AS3 developer coming to this it feels like stepping back in time to the fuzzy old days of AS1, pre strict typing, pre code hinting, pre clear, bug free and manageable code.
In Red5 you get to explicitly declare your application entry point class in a strictly typed language. The fact that you get to declare the application, to see its dependencies and inheritance chain and have the benefit of auto code completion is very reassuring. The fact that you now have a wealth of Java libraries at your disposal is pretty cool too.
In FMS you have to declare functions that can be called by clients on the client object that is received by the server side code when a user connects. I always found this a bit odd and it confused me for a while.
In Red5 you can declare functions to be called by clients as methods of the application itself, or no doubt as methods of classes that are referenced by the application. Again this just feels more ‘proper’ to me in that I’m exposing a public API for the application itself, rather than defining a list of functions that are available to a client on the server’s representation of the client object as it connects.

Differences in user experience:

When developing a Flex app in FMS I found myself at a loss what to do with the server side code I was creating. Ideally I wanted this to be a Flex Library, an ActionScript Project or something similar so that I could code my entire application from the same IDE. The problem with that approach is that you’re trying to push a square peg into a round hole, as the notion of building the project is lost with .asc files. Instead I ended up just creating a simple folder structure for the .asc files and editing them in Flash to take advantage of the code highlighting.

Developing with Red5 however is light years ahead, especially with the introduction of the Red5 Eclipse Plug-in that can actually automatically create your server and client side code for you..! Red5 server side code is a Java project, so can be edited in a Java Perspective within Eclipse. This allows you to switch between the Flex and Java Perspectives within the Eclipse IDE when editing your client and server side code. You can also actually build your Java project and even run it within Eclipse by publishing a local instance of the server.

It seems slightly counter intuitive to me that its the free product here that’s obviously streets ahead of the quite pricey product. I’m not complaining though! Plus the free product is the one that is much more flexible in terms of where it can be installed as its built on Java rather than .Net. Ideal! :-)

11 Responses to “Basic SharedObject example in Flex, Red5 & Flash Media Server”

  1. tom Says:

    Thanks Rob!

    I’m currently trying to decide between FMS and Red5.
    Right now I’m leaning towards FMS because the server-side app that I need is already written (just a/v streaming) and because I don’t know the first thing about Linux, nor java!

    But in the long run (if/when I get traffic) I might need to move to Red5.
    Any pointers on getting started with Red5?

  2. robsilverton Says:

    Hi Tom,

    I’ve no idea about Linux either but I managed to pick up Java pretty quickly when playing with Red5 based on my AS3 knowledge. You can run Red5 on Windows anyway so I wouldn’t worry about Linux for now.

    As for which one to go for, I’m currently undecided. Red5 is a better dev experience once you get it working, and I now prefer coding serverside in Java rather than in JavaScript, but I have reservations about using it on a big commercial project where things could go wrong and the support would be limited.

    These have also been recommended to me recently:

    http://www.smartfoxserver.com/overview.php
    http://www.electro-server.com/

    …so I might have to have a play when I get the chance.

    If you’re just getting started with Red5 I’d have a scan of my other posts on the subject, I think I hit just about every problem imaginable… ;-)

    cheers
    rob

  3. anon Says:

    Red5 is a very interesting project, but I’ve thrown away nearly three years of my life because of it developing a product which has never been stable enough to sell – as Red5 is constantly shifting and breaking with every build.

    One day Red5 might well be the most spectacular streaming server out there… it is incredibly feature packed, but for the forseeable IMHO I would not build anything on it where you have paying customers expecting 99.9999% uptime, or even 75% uptime.

    My honest advice to you is to either use FMS or check out Wowza. Wowza is also Java based, has only 10% of the stability of Red5, but it dramatically more reliable.

  4. robsilverton Says:

    Thanks very much for your input anon, Wowza has been recommended to me recently by a developer friend too so I think this will be the next media server on my list (which seems to get longer every day…) ;)

    Before I was talked into having a play with Red5 my plan was to wait for the version 1 release. I learned a lot from the experience, but I think for the meantime there’s probably more value in broadening my experience than digging deeper into Red5.

  5. schrodinger09 Says:

    Hi folks,

    Red5 just released version 0.8 RC2; what’s the big difference between it and other releases: stability:
    http://www.red5.org/

    We added working unit tests, and end-to-end flash system tests that are run on every checkin. We will continue that for future releases.

    The other thing that can help Red5 get to the level the community needs is more tutorials like this one. Great work Rob!

    If you had a moment to update the link about Red5 0.8 RC1 to point to RC2 that’d be great — RC2 fixes a lot of the RC1 stability issues.

    Thanks,

    - Art Clarke

  6. Good Sample Code for Shared Objects with Red5 and Flex « Xuggle Says:

    [...] Good Sample Code for Shared Objects with Red5 and Flex Rob Silverton has put together some sample Flex code for using Shared Objects with a media server, and then shown sample code for the server-side application in both Red5 Java and Adobe Flash Media Server Server-Side ActionScript.  It’s pretty simple, but powerful.  Check it out. [...]

  7. paktua Says:

    dear robsilverton, i’m sory to ask this, but i can’t make your example run, i tested it, the connection is working, the application print that conection success, but i try to write something and hit the send button and nothing happen.

    i’m very much newbie on red5 and java programing, please tell me if i miss something.

    thank you

  8. denden123 Says:

    sir, is this code only work at red58Rc1 ? because i’m using red507, the connection is connected, but the chat is not working.

    btw i’ve read an issue that shared object on red507 is not working, is it true?

  9. robsilverton Says:

    hi paktua/denden,

    I’m afraid I haven’t had my head in the Red5 world for a while now due to a rather large and ongoing project, so I’m not sure about the shared object problem you mention.

    If you’re not having any luck with red5 v0.7 I’d suggest upgrading to red5 v8 rc1 or the latest version and trying again. Difficult to say what the problem might be based on the above to be honest, as in my experience it can be any number of things.. :S

    best
    rob

  10. sharedObject basic on red5 | arifwicaksono Says:

    [...] simple and basic sharedObject example given by robsilverton here, i’ve stuck with red5 sharedObject for several days, try to understand how it works and [...]

  11. denden123 Says:

    dear rob,

    i finally can make your code works,so i add to make a login action at the code, i can make the user connected or get the userlist (i’m combine your serverside code with clientmanager code)

    but i have a problem i can login as a new Chat-User and the Userlist is getting updated, however, it only updates the userlist for the one user who has just logged in.So the userlist for all the other users, who had already logged in before, is not getting updated.

    can you tell me how to syncrhronize the userlist to other users who had already logged in?

    i really appriciated if you want to help me,
    thanks you very much.

Leave a Reply