I am somewhat late perhaps, with my profession of love, but last week, we launched a little Valentine’s Day campaign for our clients. We used a great open-source alternative to Flash Media Server (FMS), namely Red5. Red5 has been around for a couple of years now, but this was only the second time I actually got to work with it and I must say that its ease-of-use is simply amazing. Installing the service is pretty straightforward and once you have your server up and running, you can develop applications in a matter of minutes.
Installing the Server
You can download the Red5 server package from Google Code (current version: 0.9.1 final). Once it’s downloaded, just run the executable and it will automatically install the files and then prompt you for an IP address and a port number. When setting up the server for local testing, I just use 127.0.0.1 and 8080.
Once the installation is finished, you might want to run services.msc and check if the Red5 service is installed correctly and is running. With that done, we can open up a browser window and browse to http://127.0.0.1:8080 and you’ll see the Red5 gateway. Here you can test if Red5 is running properly and you can install some of the default applications that come with the Red5 installer. Red5 runs on Java and these applications are great for people like me, who don’t know Java and want to get right down to the good stuff. You can just install one of these basic applications (I recommend installing the oflaDemo app) and once it’s installed, your front-end application can just connect through one of these.
So how about we get to the interesting bit?
Creating your First App
Creating an app in Red5 isn’t too hard if you’re familiar with the NetConnection and NetStream classes, but you can save yourself a lot of trouble if you think your application through.
I created two classes that, in my opinion, came in quite handy:
- Red5Connection, the Red5Connection class is a subclass of Flash’s NetConnection class and is pretty much just a class that handles the events that a NetConnection might throw at you.
- AbstractClient, just an extension of a Sprite or DisplayObject. Since we needed three entirely different clients (a broadcaster, a subscriber and a simple chat client, which displayed messages sent by the other clients), it was important to have one base class with the basic logic for all three clients.
Both of these classes are available for download here: Red5 Example files
When working with Red5, you’ll use two things often: a NetStream and a Remote SharedObject. The NetStream is used to broadcast or stream video footage. A SharedObject is used to store or retrieve data remotely. We use this to broadcast data to all connected users (could be used in a simple chat client for instance)
Here’s how you would normally use the NetStream object:
_ns = new NetStream(_nc);
// get the first camera
// set the max bandwidth used by the stream to infinite and the quality to 95 (0-100)
_cam = Camera.getCamera("0");
_cam.setQuality(0, 95);
// get the first microphone and if there is one, make sure there's no loopback nor echos
_mic = Microphone.getMicrophone(0);
if(_mic){
_mic.setUseEchoSuppression(true);
_mic.setLoopBack(false);
}
// attach microphone and camera to the netstream object
_ns.attachCamera(_cam);
_ns.attachAudio(_mic);
// publish the NetStream to Red5.
// the publish method uses two parameters, a name and a type
// the name is used to identify the netstream
// the method determines whether the netstream will be recorded, appended to an existing stream or just streamed live
// possible values are "record", "append" and "live"
_ns.publish("broadcastExample", "live");
If you wanted to stop a broadcast, you could use these bits of code:
_ns.attachCamera(null); // just removes the camera _ns.attachAudio(null); // just removes the microphone _ns.play(false); // stops the broadcast but leaves the NetStream intact _ns.close(); // closes the NetStream altogether
And this is how we use the SharedObject’s send() method to send data to and from all connected clients
if (_so) _so.send("methodName", parameters);
An implementation of this code can be found in the Broadcaster and Subscriber files in the zip file I provided.
To recompile the .fla files on your computer, you will need to download De Monsterdebugger and adjust the class path settings via File > Publish Settings…
Furthermore, you will need to run Red5 locally (or adjust the RED5 constant in AbstractClient.as) and have the oflaDemo app installed in order for the files to work.
Well, that’s it for me, hopefully I was able to persuade at least some people to look into Red5.






