P2P in Desktop, Mobile, and Tablet Flex Apps

Using the open source Flex SDK, developers can easily build desktop, mobile, and tablet applications that use Peer to Peer (P2P) communication. I’ve created a video that walks through demos and code illustrating how to use the P2P APIs in Adobe AIR applications. Check it out:

Grab the code for the demos in the video from github:

Just as the video shows, it’s incredibly easy to use the P2P APIs. Here is a quick walk through. First create a new NetConnection that is connected to “rtmfp:” like so:

localNc = new NetConnection();
localNc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
localNc.connect("rtmfp:");

In the netStatus event listener wait for the “NetConnection.Connect.Success” event and then set up the NetGroup:

private function netStatus(event:NetStatusEvent):void
{                        
    switch(event.info.code)
    {
        case "NetConnection.Connect.Success":
            setupGroup();
            break;
    }
}
 
private function setupGroup():void
{
    var groupspec:GroupSpecifier = new GroupSpecifier(GROUP_ID);
    groupspec.ipMulticastMemberUpdatesEnabled = true;
    groupspec.multicastEnabled = true;
    groupspec.routingEnabled = true;
    groupspec.postingEnabled = true;
 
    // This is a multicast IP address. More info: http://en.wikipedia.org/wiki/Multicast_address
    groupspec.addIPMulticastAddress("239.254.254.2:30304");
 
    netGroup = new NetGroup(localNc, groupspec.groupspecWithAuthorizations());
    netGroup.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
}

Now handle a “NetGroup.SendTo.Notify” event when a message is received over the P2P connection:

// in the netStatus switch block
case "NetGroup.SendTo.Notify":
    var data:Object = event.info.message;
    break;

And finally to send a P2P message to everyone who is listening simply do:

netGroup.sendToAllNeighbors(objectToSend);

That’s it! Super simple and super fun! :)

Thanks to Tom Krcha for all of his great blogs on how to do this. Also thanks to Mark Dong and James Li (Flash Platform Evangelists in China) for helping me build P2Pong.

This entry was posted in Adobe AIR, Flex, Mobile and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Benyamin

    That’s pleasure James,

    Does it work if you deliver native iPhone app with iPhone Packager too?

    Ben

    • http://flashrealtime.com Tom Krcha

      Hi Benyamin,

      yes, absolutely. P2P works across all devices that support AIR 2.5 and Flash Player 10.1, even apps packaged over PFI. Have a look here: http://www.flashcomguru.com/index.cfm/2010/10/4/multicastchat

      Best,

      Tom

      • http://www.flex-tutorial.fr Adobe Flex Tutorial

        Just to clarify, even if that’s still temporary, the PFI is still only for AIR 2 and not AIR 2.5 so you wont be able to use AIR 2.5 APIs on your iOS device but i believe the P2P stuff was introduced in AIR 2

        Fabien

        • http://renaun.com Renaun Erickson

          It definitely works across devices Android, iOS, and PlayBook, I use it all the time and demo it regularly.
          http://renaun.com/blog/2010/12/pickquick-source-code/

        • Umair Aamir

          Hi! I am facing a problem using cirrus. My application throws event response of connecting NetGroup as Rejected. NetGroup.Connect.Rejected when my flashplayer global peer assisted network settings are set to Always Ask. It don’t show any popUp. What may be the reason

      • aubrey fletcher

        Will this work in a desktop browser with flash 10.1 to communicate with a mobile device running AIR2.5 or do you need AIR on all devices? I have a flash project running in a browser from my desktop (a kiosk with all files on the cpu hard drive.) I would like to create a remote that can control it through a LAN.

  • Benyamin

    Hey Tom,

    Thanks, I saw your multi screen sample app and was awesome.

    Have you tried RTMP with LCDS back-end by an app on iOS device too?
    I need to know this feature works in this way (cross-compiling) on iOS devices too or not.

    Great weblog,

    Ben

  • http://dmartin.org Dan Martin

    This is a really cool sample app. But in many ways, I think these easy prototypes are disingenuous because they give everyone the sense that P2P in Flash Player is an easy proposition. As someone trying to implement it beyond the prototype stage, using Adobe tools has become much more a headache than these easy tutorials imply.

    First, Cirrus/Stratus prohibits commercial use. So it is out the door as soon as you want to move beyond the fun prototype stage.

    Next you’ll look to LCCS as a commercial option. But LCCS has a per-user-per-connection-minute cost. If you read the LCCS pricing FAQ, http://forums.adobe.com/servlet/JiveServlet/download/2292267-22393/LCCS_Pricing_FAQ.pdf you’ll see the RTMFP price is $0.01 USD per hour. This may sound reasonable, but it really isn’t if you ever achieve the success of something like Chat Roulette (or even anywhere in that stratosphere). For example, if you had 10,000 concurrent users, you are paying $100 / minute, or $14,400 per day! You’d better have deep pockets or a good business model, or you’ll be in massive debt quick. Worst of all, if you are developing something like Chat Roulette, you may have no idea how much money you’ll expect to spend. Do you think the author of Chat Roulette knew it would be such a huge success? Most successful P2P products could not be successful at $.01 per user-minute.

    Once LCCS looks unattractive, you can turn your attention to the Flash Media Server products. You’ll quickly learn only FMS Enterprise supports RTMFP. Now you are paying for a product that does tons of nice stuff, when all you want is a simple RTMFP rendezvous service. I probably am not allowed to publicly say the price of FMES, but rest assured it’s in the range that anyone not an “Enterprise” will choke when they hear.

    It would be nice if Adobe offered an RTMFP rendezvous-only solution at a fitting price. Most people that purchase FMES will never use RTMFP, and many of us that want only RTMFP rendezvous can’t afford FMES.

    • http://www.jamesward.com James Ward

      Hi Dan,

      Thanks for the feedback. All of these demos actually don’t use LCCS or Cirrus/Stratus. They use pure local network multicast P2P. So they are only useful for P2P that you want to do on the local network. I think all three demos I showed are good places where you do not need anything other than local network multicast. I realize there are other use cases where P2P beyond the local network is necessary.

      What do you think?

      -James

      • http://dmartin.org Dan Martin

        I honestly didn’t realize Flash could do P2P without a rendezvous, so that’s good to know. Everything I’ve seen so far required a rendezvous, and I guess I didn’t pay enough attention to your example. So, I want to withdraw my criticism. :) It really is a cool example, and I don’t want to take away from that with my own petty issues with RTMFP.

        Your point does makes me wonder why RTMFP is included in FMES only. Enterprises always have a corporate LAN and, by your example are one of the few environments where a LAN only P2P solution makes sense without a rendezvous server. That they are the ones targeted by FMES seems odd.

  • http://blog.six4rty.ch Tiago Dias

    Very nice article James, short, easy to understand and works like a charm..
    Thanks again for such a great post!

  • http://agileui.blogspot.com Rob

    I must be doing something wrong, but if I download P2Pong the code and compile it and install it on two Android phones that are connected to the same WI-FI network, this doesn’t work for me. Am I missing some step here?

    • http://www.jamesward.com James Ward

      It’s possible that your network is blocking P2P / Multicast traffic. I’m not sure how to test that. Can you try another network?

  • Pingback: @renaun posts: PickQuick Source Code

  • Pingback: Remotair at Jozef Chúťka's blog

  • Kyle Adams

    How would you address this from an enterprise use case standpoint? Security admins would have a heart attack if I mentioned I was creating a P2P application. Are there alternatives?

    • http://www.jamesward.com James Ward

      P2P has certainly been abused. But the core of P2P can definitely be useful for many things. I’d probably say that it was a multicast application not a P2P application. :)

  • Henry

    How can we set the orign-edge mode to build large-scale platform to support above 2-3 million people online ?

    • http://www.jamesward.com James Ward

      Good question. I’m not really sure. You might want to ask Tom Krcha:
      http://flashrealtime.com/

  • http://resistdesign.com Ryan Graff

    Scanned the docs real quick but didn’t see it, how many netgroups can you have per netconnection?

  • thomas

    Hey James had u worked on any multi user whiteboard using this rtmfp?

  • Pingback: Building multiplayer games | Astraeus



  • View James Ward's profile on LinkedIn