Tom Krcha's FlashRealtime

Hey amigo!
I am Tom Krcha, Gaming Evangelist at Adobe. These are my notes


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:

70 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.
    [email protected]
    backup: [email protected]
    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

  46. Hi Tom,

    I am new in FMS so I don’t have much knowledge about FMS. I have gone through your presentation and blog, after you presentation, I have learned some things about FMS.

    Now, I am working on a project and developing peer to peer video(1X1) communication and into the project, I am using flash cs4 and FMS4, but i am facing some problem regarding NetGroup object. and error is “1046: Type was not found or was not a compile-time constant: NetGroup”. i am not getting solution for it.

    if possible then please help me!.

    Thank
    Sushil kumar.

    Comment by Sushil — October 4, 2010 @ 4:00 pm

  47. Hi !
    Very good tutorial but I am using Flash CS4 and after changing your code, the connection to Stratus is fine then I get a NetStream.Connect.Success and a NetStream.Play.Start but the typed text is not sent after clicking the button.
    Can you help?
    Thanks !

    Comment by Mar — November 24, 2010 @ 6:42 pm

  48. Tom,

    I am using the P2P sample code from adobe and it compiles fine! However, when I upload to my web host, I get errors (Error# 2032) whenever I try to connect! Since the Stratus service recently changed names to Cirrus, has the server address changed from “rtmfp://stratus.adobe.com” to something else as well? Also, the code that Adobe provides has the connectURL set to “rtmfp://p2p.rtmfp.net/” which is different from yours. Nothing seems to be consistent, and there is not much information on which connect server URL is correct. This is frustrating to say the least!

    Comment by Matt — November 30, 2010 @ 5:00 pm

  49. Tom,

    I am using the P2P sample code from adobe and it compiles fine! However, when I upload to my web host, I get errors (Error# 2032) whenever I try to connect! I think it might have something to do with the WebServiceUrl not being set. The problem is that there isn`t any help on what exactly this URL is, or where to find it. Is it my domain name? I have uploaded the reg.cgi Python script to the CGI bin, but what do I have to do next? The help says “You should override the AbstractIdManager class to implement your own peer ID exchange mechanism—using, for example, XMPP, Google Apps, or the Facebook framework.” Is all that really necessary?

    Comment by Matt — November 30, 2010 @ 5:15 pm

  50. Hi Tom and thks for this tutorial!

    I developped a Chatroulette like but unfortunately I can’t attach microphone’s user to the NetStream.

    Every time, sound is detected, it crash the application on subscriber side :(

    I tryied a lot of things but nothing works …

    Are u aware of that problem ? I must do something wron but i can’t figure what

    Any tips or help will be great pleasure!

    Thks in advance
    Gilles

    Comment by Gilles — December 13, 2010 @ 12:15 pm

  51. How can we make this into streaming a camera feed? I’m a newbie at flash and I am desperate to create a p2p camera sharing page. I would love some help

    Comment by Peter — December 29, 2010 @ 7:59 am

  52. Hi,

    I tried to connect. Pressed the button connect. Nothing happens. I deactivated antivirus, firewall. Still nothing.

    ????

    Comment by Omu Neted — January 21, 2011 @ 12:27 am

  53. Solved the issue.
    Here is the solution. If you can’t connect, with no error message no nothing. Take to code below the movie(the P2P.mxml file) and edit it.
    (I used Flash Develop, it’s free)

    Change the address from
    “rtmfp://stratus.adobe.com/” to “rtmfp://p2p.rtmfp.net/”

    and add your developer key.

    So yeah you should make an account.

    Flash Develop has not given me any errors with the code.

    Comment by Omu Neted — January 22, 2011 @ 2:50 am

  54. Your content articles are truly great, I bookmarked it on digg for you!

    Comment by Electricians Minnesota — January 27, 2011 @ 11:56 pm

  55. Could be the top paper I read this week!!

    -Sincerely,
    Oscar

    gume

    Comment by Nolan Brandt — February 4, 2011 @ 8:23 am

  56. Hi - I am looking for a programmer who is knowledgeable in Cirrus… I need to flush out what I can do for an idea I have for an application.
    Have anyone to recommend?
    Thanks!

    Comment by Ron — February 9, 2011 @ 7:11 am

  57. hi - thanks for posting. i cant connect b/c of firewall, but when i try to just text locally by connecting to just “rtmfp:” i can connect, but the onPeerConnect callback never fires when i try ini the receive stream. have u run into this? thx :-)

    Comment by Kiyo — June 2, 2011 @ 4:10 am

  58. hello,
    very good video, this works at the same way in cirrus?

    Comment by soteldo — July 8, 2011 @ 4:35 pm

  59. Hi Tom! How to transfer the coordinates of the Movieclip to another user to see the clip on the move? And accordingly, it was obvious that the motion Movieclip to another user. Something resembling a game of ping-pong or billiards …

    Comment by garymar — July 13, 2011 @ 10:32 am

  60. This is good video, Thank you

    Comment by Thanit — August 1, 2011 @ 1:10 pm

  61. How do I setup 2 videos displays? One for me and one for the partner, to make a camroutlette?

    Comment by Anonymous — August 19, 2011 @ 5:22 pm

  62. How do I setup 2 displays to make a camroulette?

    Comment by Hunter — August 19, 2011 @ 5:24 pm

  63. Thanks a lot! Very useful tutorial. Just the exact information that I’ve been looking for.

    Comment by Vic — October 2, 2011 @ 12:43 am

  64. Your place is valueble for me. Thanks!…

    Comment by pacquiao vs marquez live streaming — November 11, 2011 @ 2:15 am

  65. Hi, what do you think, what it could be?
    I connect to port 1935 with my Cumulus service (C++ helper library/service), get p2p_id successfully, but can’t initialize the stream on specific network (only one, on others work fine). Can it be network-based UDP-blocking or something?

    Comment by Sergej — November 29, 2011 @ 7:47 am

  66. very nice. what a advanced tutorial.
    Thanks for this great video.

    Comment by naresh kumar — December 20, 2011 @ 8:25 am

  67. Great tutorial, thanks for breaking it down and making it so simple! I’m trying to get more accustomed to using Flash.

    -Michelle

    Comment by acting classes hollywood — December 29, 2011 @ 9:36 pm

  68. You said:
    “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”

    How this can be done. If i try to communicate a php file.. then that too can be revealed by decompilation

    Comment by Vishwas — May 1, 2012 @ 3:18 pm

  69. Thanks a lot! Great tutorial. Just the exact information that I have been looking for.hj

    Comment by Didi Games — September 20, 2012 @ 3:39 am

  70. Hi Tom,

    Currently i am working on creating a p2p mobile game using pure as3 connect via rtmfp. The problem that i facing now is i can’t display the player name when they join the game ( only can display PeerID) do you have any idea how to fix it? or any better recommended alternative way to do it?

    case “NetGroup.Neighbor.Connect”:
    var peer:Object = new Object();
    peer.id = event.info.peerID.toString();
    peer.label = inputText.text + (event.info.peerID);
    peerList.addItem(peer); break;

    Comment by Steven — January 11, 2013 @ 6:23 am

RSS feed for comments on this post. / TrackBack URL

Leave a comment

Comment moderation is enabled. Your comment may take some time to appear.