Tom Krcha's FlashRealtime

Hey amigo! These are my notes. I'm Platform Evangelist with Adobe.


Basics of P2P in Flash

May 31st, 2009

This videotutorial explains the basics of working with P2P (peer to peer) in Flash Player 10.

I made this tutorial with focus on simplicity of this fantastic feature of Flash. There are bunch of articles and examples in the world showing how to create your own P2P app, but most of them are very complex or hard to catch.

In this tutorial you’ll get idea how to connect two different peers and exchange data between them with ActionScript 3.0 and Flex only.

Prerequsites:
- familiarity with ActionScript 3 and Flex Framework
- Flash Player 10 Debug version installed download
- Flex Framework 3.3+ download

P2P in Flash

Videotutorial: http://flashrealtime.com/tuts/p2p-in-flash.html (20 min.) 
Try: P2P demo (run in two browsers, explained in video)
Source: download P2P.mxml.zip

Note: Programatically the main P2P logic is not to connect peers together just via NetConnection, but by defining peerIDs to NetStream.
PS: don’t forget to change your Flex Compiler settings to use Flash Player 10
Btw: nice P2P Mill Game done by my friend Pavel Šimek http://nestor.cz/mill/ 

Good Luck!

Facebook comments:

45 Comments »

  1. Awesome tutorial man!

    Comment by Lee Brimelow — June 1, 2009 @ 11:16 am

  2. Thanks Lee! Next tutorial will be on P2P with AFCS.

    Comment by tom — June 1, 2009 @ 4:44 pm

  3. Excellent tutorial Tom, thanks for the source especially. Got an API key and will be cracking away at this next week. Nice one

    Comment by Rob McCardle — June 3, 2009 @ 5:32 pm

  4. Nice, but does anyone know when we can build commercial products with this? Stratus is a beta s/w Adobe labs project and has been for over 6 months now. Some people have suggested it would be in FMIS 3.6 but given that Audio Echo Cancellation ended up in the special Flash player in Adobe Connect Pro maybe this will be a Adobe web service? Either way I don’t mind paying as long as something happens :-)

    Comment by Andi Freeman — June 4, 2009 @ 9:58 am

  5. Nice tutorial, but I can’t get it to work. I can connect but not send any text through. How come? No errors or anything, there is just not coming anything through.

    Comment by Simone — June 10, 2009 @ 12:09 pm

  6. Andi> Stay tuned :)

    Simone> It’s pretty weird - when you have it on one computer - it should work … does this SAMPLE work for you? http://labs.adobe.com/technologies/stratus/samples/

    Comment by tom — June 11, 2009 @ 2:07 pm

  7. when trying to connect using your sample i get :

    ArgumentError: Error #2126: Es muss eine Verbindung zum NetConnection-Objekt bestehen.
    at flash.net::NetConnection/get nearID()
    at P2P/ncStatus()[/Users/tomkrcha/Documents/Adobe Assets/DemoWorkspace/P2P/src/P2P.mxml:28]

    :D

    Comment by dl — June 23, 2009 @ 5:50 pm

  8. So basically - you click Connect button and get this message? Make sure you haven’t filled anything into TextInput field

    Comment by tom — June 23, 2009 @ 6:32 pm

  9. Oh right - gotcha … if you get Error: NetConnection must be connected - than it means, that you didn’t connect to stratus - so you are behind the firewall, which is blocking UDP. See function ncStatus … there you can write condition if(event.info.code==”NetConnection.Connect.Success”) and then ask for nearID, otherwise - it’s not connected, so nearID can’t be get.

    Comment by tom — June 24, 2009 @ 8:46 pm

  10. Either way I don’t mind paying as long as something happens

    Comment by hootoo — July 7, 2009 @ 8:39 am

  11. Gateway S-7200C AC adapter, thanks

    Comment by hootoo — July 7, 2009 @ 8:41 am

  12. That’s probably not the problem of the HW, but it’s settings. UDP communication must be open - also some important ports like 1935 and high UDP ports 50000+

    Comment by tom — July 9, 2009 @ 12:58 pm

  13. question - how do we deploy this without giving away our developer key to the world?

    Comment by Ariel Jakobovits — August 27, 2009 @ 2:57 am

  14. Well - basically you can hardcode this into your Flash SWF, but it can be decompiled. What you can do is to ask your server for this developer key and it can return you one via HTTPS

    Comment by tom — September 2, 2009 @ 11:44 am

  15. can’t run the source code, i get 5 errors

    Severity and Description Path Resource Location Creation Time Id
    1119: Access of possibly undefined property DIRECT_CONNECTIONS through a reference with static type Class. p2p/src p2p.mxml line 36 1252517899031 11

    Severity and Description Path Resource Location Creation Time Id
    1119: Access of possibly undefined property farID through a reference with static type flash.net:NetStream. p2p/src p2p.mxml line 43 1252517899031 12

    Severity and Description Path Resource Location Creation Time Id
    1119: Access of possibly undefined property nearID through a reference with static type flash.net:NetConnection. p2p/src p2p.mxml line 28 1252517899031 9

    Severity and Description Path Resource Location Creation Time Id
    1137: Incorrect number of arguments. Expected no more than 1. p2p/src p2p.mxml line 36 1252517899031 10

    Comment by omu — September 9, 2009 @ 7:41 pm

  16. Hi omu,

    you need to set Flex Compiler settings to compile for Flash Player 10, not 9.

    Good Luck

    Tom

    Comment by tom — September 10, 2009 @ 1:33 pm

  17. [...] If you are not familiar with programming P2P apps in Flash - please follow my video-tutorial. [...]

    Pingback by Deploying Flash P2P app on Facebook — FlashRealtime.com — October 14, 2009 @ 4:30 pm

  18. I get, NetConnection event: NetConnection.Connect.Failed, I use proxy server in the organisation, is that blocking the communication?

    Comment by Rishi — November 3, 2009 @ 8:56 am

  19. Hi Rishi, yes it could be.

    Comment by tom — November 13, 2009 @ 12:29 am

  20. [...] first, shorter video tutorial was authored by Tom Krcha of Adobe and guides you through building a simple P2P application [...]

    Pingback by P2P Features in Flash Player 10 | DevWebPro — December 10, 2009 @ 12:28 am

  21. i press on connect and nothing happends :-?? any solution?

    Comment by paul — December 16, 2009 @ 9:36 am

  22. ArgumentError: Error #2126: NetConnection object must be connected.
    at flash.net::NetConnection/get nearID()
    at P2P/ncStatus()[/Users/tomkrcha/Documents/Adobe Assets/DemoWorkspace/P2P/src/P2P.mxml:28]

    I tryed tot test your demo application but it does not work for me. I got that error.

    Ps. my windows firewall is set off.
    Any other solution?

    Comment by alexei — December 16, 2009 @ 11:59 am

  23. Any comment about using this Flash technology for secure data communication in industries with security compliance requirements? This means securing over the wire, ensuring the data is never on a server (just P2P), man in the middle and impersonator attacks. Have the protocols been security reviewed, or certified in any way? Thanks.

    Comment by Jim — December 22, 2009 @ 3:42 pm

  24. @Jim - RTMFP is using 128-bit AES encryption.

    Comment by tom — March 10, 2010 @ 10:38 pm

  25. Tom, where can I call you as I need to speak with you concerning a project that I am funding and seek programmers?

    Regards.

    Aaron Ariel C.
    aaron@capitalequitypartners.com
    backup: aaronarielcarrion@gmail.com
    Tel. International Costa Rica (506) + 8819 4700
    Tel. USA: 914 + 401 5204
    Fax USA International + 270 + 447 3738
    Internet Telephone: http://www.skype.com
    User: aaroncapitalequitypartners

    Comment by aaron — March 25, 2010 @ 6:09 am

  26. Hai Sir,

    How to make video streaming when more than 2 peers communicate in stratus 2.How to switch the display when any peer want to communicate?if i’m using two display,how can it possible in netgroup?

    Ad.Thanx

    Comment by Satheesh — March 25, 2010 @ 5:42 pm

  27. Please refer to my article here. http://www.flashrealtime.com/simple-chat-p2p-netgroup-rtmfp/ Instead of NetGroup you use NetStream.

    Comment by tom — March 26, 2010 @ 12:09 pm

  28. good tutorial tho the sounds a bit too low.

    Comment by 7 — April 14, 2010 @ 5:55 am

  29. good tutorial tho the volumes a bit too low.

    Comment by 7 — April 14, 2010 @ 5:55 am

  30. Doesn’t work for me either. Running on a fully-open Mac with Chatroulette working no problem. Trying to run this code just gives:

    ArgumentError: Error #2126: NetConnection object must be connected.
    at flash.net::NetConnection/get nearID()
    at P2P/ncStatus()[/Users/tomkrcha/Documents/Adobe Assets/DemoWorkspace/P2P/src/P2P.mxml:28]

    Comment by Nello — April 23, 2010 @ 2:55 pm

  31. @Nello: That means that you are trying to establish stream without being connected to the server. You need to receive NetConnection.Connect.Success before

    Comment by tom — April 23, 2010 @ 10:47 pm

  32. Hi
    Great tutorial
    But what about Flash ?
    How can i change your code for adobe Flash CS4 ? Is it possible ?

    I made everithing as in your tutorial but in Flash CS4 IDE
    and i could not receive onPeerConnect (starting connection to Stratus is OK)
    Can i show you my code ?

    Comment by Ilay — April 25, 2010 @ 11:19 pm

  33. 21 — high [b]low cost medication diovan hctz[/b] variants of c-reactive prevaccination (crp) may asymmetry a person’s mouthpiece for rociado attack and death, but wrongfully for stroke, a grey isopropanol has found.

    Comment by Anonymous — May 5, 2010 @ 4:27 pm

  34. However, a long-term [i]tramadol c o d delivery[/i] of incandescent cephalexin in wistar canids of wks has heartily resteled that sarapin is carcinogenic.

    Comment by purchase generic lasix online — May 5, 2010 @ 4:29 pm

  35. Aseg

    Comment by Anonymous — May 5, 2010 @ 8:35 pm

  36. For example, in australia, where a surmise indigestible of 1.

    Comment by Anonymous — May 5, 2010 @ 9:14 pm

  37. [...] Peer to peer networking – I’m really looking forward to this for multi-player Flash games. [...]

    Pingback by HTML5 Vs. Flash. What You Haven’t Heard. — May 14, 2010 @ 2:09 pm

  38. Trying demo nothing happens while “Connect button” is being pressed.
    Is it possible to develop own randevouz server?

    Comment by xmlelement — June 29, 2010 @ 9:00 am

  39. would also like to see this done in CS4/CS5. So tired of everything being done in Flex.

    Comment by Charles — July 10, 2010 @ 2:21 am

  40. [...] 600 is it a flash based mmo? I’m not that good in as3 but i can give some links m8 helps Basics of P2P in Flash — FlashRealtime.com FlashRealtime.com devboy’s AS3-P2P-LIB at master - GitHub Create a simple P2P chat application you [...]

    Pingback by Looking for team member for AS3 (Flash) project — July 27, 2010 @ 8:50 am

  41. When i compile your code, its not working? What is exactly you did then?

    [sun@example src]$ /home/sun/Downloads/flex/sdks/3.0.0/bin/mxmlc -target-player=10 flashs.mxml
    Loading configuration file /home/sun/Downloads/flex/sdks/3.0.0/frameworks/flex-config.xml
    /var/www/html/flash/src/flashs.mxml(38): Error: Access of possibly undefined property nearID through a reference with static type flashs.

    myPeerID = this.nearID;

    /var/www/html/flash/src/flashs.mxml(46): Error: Incorrect number of arguments. Expected no more than 1.

    sendStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);

    /var/www/html/flash/src/flashs.mxml(46): Error: Access of possibly undefined property DIRECT_CONNECTIONS through a reference with static type Class.

    sendStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);

    /var/www/html/flash/src/flashs.mxml(53): Error: Access of possibly undefined property farID through a reference with static type flash.net:NetStream.

    farPeerID = callerns.farID;

    /var/www/html/flash/src/flashs.mxml(65): Error: Incorrect number of arguments. Expected no more than 1.

    recvStream = new NetStream(nc,farPeerID);

    [sun@example src]$

    Comment by Rabbi — July 27, 2010 @ 7:27 pm

  42. how my flex application connects with my server side java application after connection with stratus

    Comment by usi — August 17, 2010 @ 1:23 pm

  43. very very good !!!

    Comment by yacteka — August 25, 2010 @ 2:02 pm

  44. i have the same problem. 5 errors in the code !

    Comment by Via — August 25, 2010 @ 4:38 pm

  45. @Via - set the Flex Compiler to 10.1.0

    Comment by tom — August 25, 2010 @ 6:36 pm

RSS feed for comments on this post. / TrackBack URL

Leave a comment