Multicast Messaging Example Using HydraP2P
If you are looking for a way to have devices talk to each other with out the devices knowing about each other check out this post. There are various options but in this post I will talk about using Flash Player’s multicast ability. The underlying API for this Flash Player 10.1 feature is NetGroup and RTMFP groups. Basically it allows you to connect to a multicast ip and send messages for any one listening to receive. This is not a direct message to a specific device so beware the messages are broadcast-ed to all devices on the network. You can configure what multicast ip (anything above 241.0.0.1) and the port (make it unique so go high >1024) which lessens the chance for someone to sniffer the packets. For most cases it won’t matter and you could do your own in message logic to lock down your data flow.
Since any multi-user/device application requires a bit of logic to handle users, channels of messages, etc… I choose to try out the HydraP2P library. Dominic Grafen has created a library to abstract out the connecting and handling of users and channels to make multi-user apps a bit more manageable, he has a basic AS3 chat example here. His example uses Stratus service and will work outside of multicasting. What I wanted to do was use the HydraP2P library for just RTMFP multicast and provide an example in Flex 4.
Ok, enough of the introduction lets look at some code. You can grab the source as a Flash Builder 4 fxp project file: MulticastTest.fxp.
The project has two main applications in it.
MulticastTest.mxml is example by fellow Adobe Platform Evangelist Tom Krcha. I modified the chat application into the
HydraP2PTest.mxml application to use HydraP2P classes.
The first thing to get HydraP2P’s
HydraService setup to use multicast is to pass in the multicast service string: “rtmfp:”. When using Stratus you would provide the full Stratus url along with developer API key. But just “rtmfp:” tells the NetConnection to use the local multicast address set in the
Once I have the HydraService configured with the right server string then I need to create a channel, HydraChannel. This channel then gets passed in the GroupSpecifier that is configured for a particular multicast address. Check out the code below:
var specifier:GroupSpecifier = new GroupSpecifier("hydrap2ptest/groupOne");
specifier.postingEnabled = true;
specifier.ipMulticastMemberUpdatesEnabled = true;
multicastChatChannel = new HydraChannel(hydraConn, "ch2", specifier, true);
The things you need to know about HydraP2P are that its made up of a service, channels, and typed message commands. The idea is to have the developer create custom commands and command creators to handle parsing of the messaging into typed commands. In my case I created a
TextCommand class that exposes a string friendly
text property. The
HydraService needs to know how to parse incoming messages and this is were the
TextCommandCreator comes into play. You’ll see above that I created a instance of the
TextCommandCreator and added it to the
TextCommandCreator looks like this:
* @author Renaun Erickson - renaun.com
public class TextCommandCreator implements IHydraCommandCreator
public function createCommand(type:String, timestamp:uint, userId:String,
if (type != commandType)
throw new Error("CommandTypes do not match!");
var command : TextCommand = new TextCommand();
command.userName = info.userName;
command.text = info.text;
command.userId = userId;
command.senderPeerId = senderPeerId;
public function get commandType():String
With typed commands now in the message handling event I can know exactly what commands I want to handle for what application logic. Here is my message handler method in the HydraP2PTest application:
if (event.command.type == TextCommand.TYPE)
var command:TextCommand = event.command as TextCommand;
Thats basically all the setup to use HydraP2P library. All those event listeners are what drive the application to change
disconnected state, notify of users joining/leaving, and posting chat messages. Check out the source code and have fun with it.
NOTE: I created my own TextCommand, as I wanted to know how things work, but if you look at HydraP2P it does come with some basic chat classes to get you up and running.