P2P Gaming Libs from my MAX 2010 session
November 24th, 2010
Update 3/18/2011: Source-code is available here: https://github.com/tomkrcha/RealtimeGameLib
Update 11/28/2010: Library now accepts variable group name, so you can create more instances. Download updated library below. Example: var game:P2PGame = new P2PGame(serverAddress,groupName);
Lot of you guys are asking for libs I’ve used in my Adobe MAX 2010 session Building P2P Multiplayer Games.
It’s still work in progress, but you can basically grab SWC libraries here with example.
I don’t want to publish source now as it needs several changes, refactor, comments, etc. But source-code should be available soon
This library has been used for MAX Racer without no change. So it’s kickass tested stuff! But not completely finished and documented yet. So this is the disclaimer, I do not provide any guarantees.
If you want to start playing with it, go ahead. Comments are highly welcome!
This engines creates a full mesh network via DIRECT_CONNECTIONS NetStreams among all connected users. Thanks to that, you get lowest latency possible and it’s highly suitable for realtime gaming.

Create a P2PGame
function connect():void{ game = new P2PGame(SERVER+DEVKEY,"myGroupName"); game.addEventListener(Event.CONNECT, onGameConnect); game.addEventListener(Event.CHANGE, onUsersChange); game.connect(userNameStr); }
User table
This game engine maintains also user table via Distributed Hash Table technique.
First registed listeners. You have 4 different events: USER_ADDED, USER_REMOVED, USER_UPDATED, USER_IDLE (when he does nothing for certain amount of time)
private function onGameConnect(event:Event):void{ game.session.mainChat.addEventListener(PeerStatusEvent.USER_ADDED, onUserAdded); game.session.mainChat.addEventListener(PeerStatusEvent.USER_REMOVED, onUserRemoved); }
You can also listen to Event.CHANGE on P2PGame class as well, this basically means that user table has changed with no other specific info.
private function onUsersChange(event:Event):void{ // write user names // you have userNames (Array of Strings), userNamesString (String), userList (String) txtUsers.text = "Users: \n"+game.session.mainChat.userNamesString; for each(var user:Object in game.session.mainChat.userList){ //createPlayer(user) } }
Sending realtime data to P2P mesh
On P2PGame class you have bunch of functions available. Every call distributes the message to each peer in the group.
public function sendMousePositions(x:uint,y:uint):void public function sendMovement(direction:uint, down:Boolean):void public function sendPosition(position:Object):void public function sendRotation(rotation:Number):void public function sendSpeed(speed:Number):void
Then you have specific game logic messages:
public function sendStartGame():void public function sendGameOver():void
Receiving realtime data from P2P mesh:
I have implemented series of callbacks, which you can register on P2PGame class for movement, position, mouse position, rotation, speed. It should be suitable for simple games. You can extend P2PGame class to add more if you want.
public var receiveMovementCallback:Function; public var receivePositionCallback:Function; public var receiveMousePositionCallback:Function; public var receiveRotationCallback:Function; public var receiveSpeedCallback:Function;
Events
And also some events on P2PGame class:
[Event(name="change",type="flash.events.Event")] [Event(name="connect", type="flash.events.Event")] [Event(name="startGame", type="GameEvent")] [Event(name="gameOver", type="GameEvent")]
Demo and libraries
Demo is available here to try (Run in multiple windows)
Libraries with source-code of SharedMovements examples
Good luck!
Facebook comments:
39 Comments »
RSS feed for comments on this post. / TrackBack URL

thx!
Comment by tong — November 24, 2010 @ 8:36 pm
Thanks indeed.. and a very nice work mate!!
Comment by Slavomir Durej — November 24, 2010 @ 11:42 pm
Thanks for libraries! How it is possible to use P2P without Cirrus? Whether there are free analogs FMS server with RTMFP?
Comment by Dan — November 25, 2010 @ 8:57 am
looking forward for the source code
Comment by tong — November 25, 2010 @ 5:06 pm
Thank you!
Comment by Ivan — November 25, 2010 @ 10:30 pm
Hello.
Sorry, but what is com.adobe.fms.events.PeerStatusEvent ?
I can’t find that package.
Comment by Ivan — November 26, 2010 @ 5:50 am
Hello.
Sorry, but what is com.adobe.fms.events.PeerStatusEvent ?
Can’t find this package.
Comment by Ivan — November 26, 2010 @ 5:54 am
Hello! Thanks for the source.
How can we change GroupSpecifier from test123 on something else?
Comment by Ivan — November 26, 2010 @ 1:24 pm
Guys, just forgot to mention that every receive callback has first parameter peerID:String, which contains the ID of the sender. It’s obvious from the code example, but worth saying anyway.
Comment by tom — November 26, 2010 @ 5:20 pm
I’m trying to use the downloaded ShareMovement demo, but in Flash Builder I’m getting an error:
“1061: Call to a possibly undefined method addEventListener through a reference with static type com.adobe.fms:GroupChat. SharedMovements.as SharedMovements/src line 90″
I’ve set it up to compile with Flash Player 10.1/10.2 but it still complains (initially I was having an issue with it resolving GroupSpecifier).
Also how does the P2PGameLib allow clients to discover peers (there’s no webservice used to connect PeerID’s in the downloaded example), as far as I knew the only ways to make this work were:
1. Use Cirrus to establish PeerID and then use your own webservice to notify the clients of PeerIDs that they can then connect to
2. Create a link that you can email/IM to another user that contains the peerID to connect to
3. Use Stratus to coordinate connections (although the examples seem to use the RTMFP Cirrus service only).
4. Use RTMFP Groups to establish a group (but this only works on a LAN meaning the P2PGameLibrary will only work on LAN?)
Comment by Doug Daniels — November 28, 2010 @ 12:30 am
What server should I buy for DIRECT_CONNECTION? Only Flash Media Enterprise Server 4?
Comment by tr1te — November 30, 2010 @ 9:10 pm
hey just an update to my last note… looks like its working on iOS… not sure why it wasn’t working before =P
Comment by gltovar — December 5, 2010 @ 12:19 am
Thank you so much! awesome work!
Comment by Brad — December 20, 2010 @ 12:29 pm
I have been creating a little test/demo game using your P2PGameEngine (its awesome by the way!)
I am creating a new P2PGame using:
game = new P2PGame(SERVER+DEVKEY,”testing/airfighter”);
As far as i know, the second parameter is the group name, but when i run my app, i see other users connecting to it, who cant possibly be in my “testing/airfighter” group.
Am i missing something? Maybe i am confused to what a group actually does, I thought only users in the same group would be connected together…
– congrats on the MAX Racer, very cool!
Comment by Brad — December 22, 2010 @ 2:05 am
i got a failure during runtime, see below for the console output. I dont know why flash builder search the logger in the path C:\_DEV\P2PGameEngine\src\Logger.as because i linked both librarys to the project.
CONNECT: d
NetConnection.Connect.Success
NetGroup.Connect.Success
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Logger$/log()[C:\_DEV\P2PGameEngine\src\Logger.as:20]
at P2PGame/onConnect()[C:\_DEV\P2PGameEngine\src\P2PGame.as:54]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at com.adobe.fms::P2PSession/onNetGroupConnect()[C:\_DEV\P2PMessengerLib\src\com\adobe\fms\P2PSession.as:208]
at com.adobe.fms::P2PSession/netStatus()[C:\_DEV\P2PMessengerLib\src\com\adobe\fms\P2PSession.as:312]
Comment by PorkyPig — December 23, 2010 @ 5:12 pm
How about adding your source codes to a public source-control hosting service (github, code.google,..) ?
This would push forward development, easier bug fixing, allow reporting issues, …
It would also create a good overview of all the (awesome and helpful!) codes provided on this blog.
Comment by tong — January 4, 2011 @ 6:35 pm
Hi,
Is it possible for a native c++ communication to stream video to flash player using RTMFP?
Comment by Jeevanand — January 6, 2011 @ 9:10 am
Tom, please answer us about server needed for DIRECT_CONNECTION.
Comment by Dejan — January 7, 2011 @ 9:08 pm
Hi, I really like your blog.
I have a question???
how can I create separate rooms for the players???
Now all who launch my game, go in 1 room.
I hope someone will help me.
Sorry for my bad English.
Comment by Igor — January 12, 2011 @ 8:02 pm
Cool work!
Just two fast questions.
With that last update (the group name thing), can I make different groups (netgroups) in the same swf? It would be great to make a general group, and then small groups, like rooms where the games take place.
How can I use sendToNearest with you library? Or is there any other way for sending a message only to a specific peerID?
Thanks!
Comment by Lor — January 17, 2011 @ 12:02 pm
sadly this stuff is not very, there’s no source code to learn from and the swc included aren’t useful in any way as they connect always to the same thing, even with different devkeys and different group names, it’s probably bugged.
I’ve tried to recompile the sample with a different group name and it still connects to the same peers, as I can see my own mouse cursor moving from my app to the swf included as demo in this article
Comment by Eclipse — January 27, 2011 @ 9:31 pm
Hi, Please tell us when we can see the source code for this libraries?
Comment by zeropoid — February 18, 2011 @ 9:33 am
please release the source code!
we really need some references we can learn from
Comment by tong — March 3, 2011 @ 11:58 am
hi tom great stuff, just wondering if the source code for that gaming lib would be available somewhere?
thanks
Comment by __juju__ — March 14, 2011 @ 10:48 am
I got this working to move and rotate my players, but now I need to extend to be able to send more info. Is there an example anywhere on how to go about doing this?
Thanks
Comment by Adam — March 17, 2011 @ 4:01 pm
i figured out how to extend it. here’s the code:
package
{
import flash.net.NetStream;
public class AwesomeP2PGame extends P2PGame
{
public var receivePlayerInfoCallback:Function;
public function AwesomeP2PGame(serverAddress:String, groupName:String):void {
super(serverAddress, groupName);
}
public function sendPlayerInfo(obj:Object):void {
var netStream:NetStream = this.realtimeChannelManager.sendStream;
netStream.send(“receivePlayerInfo”, this.session.myUser.id, obj);
}
public function receivePlayerInfo(str:String, obj:Object):void {
receivePlayerInfoCallback(str, obj);
}
public function setReceivePlayerInfoCallback(func:Function):void {
receivePlayerInfoCallback = func;
}
}
}
Comment by Adam — March 17, 2011 @ 5:34 pm
Hey guys, I am working on that, will release that hopefully very soon. Stay tuned.
Comment by tom — March 17, 2011 @ 11:27 pm
Hey Tom, do you think if its really posible to get the same or similar quality in a VideoChat using Flash & direct_connections compared to Skype? or this is like the iphone vs other phones issue? ( they’ll never get that fluent / that good )? Thanks for your comments.
Comment by Roberto — March 18, 2011 @ 2:03 am
The source-code is available here, it’s initial commit, more to come: https://github.com/tomkrcha/RealtimeGameLib
Comment by tom — March 18, 2011 @ 7:03 pm
hey Tom, thanks for releasing the source, much appreciated!
Comment by Brad — March 23, 2011 @ 8:54 pm
Hi Tom,
I have a question while i tested
http://flashrealtime.com/demos/p2pfileshare2/P2PFileSharing.html in different comuter,it does not work but it works well in two tabs of a brower in a computer.
I have no idea about it,i i want to build an application similar to that one,i need to build my fms,right?
Thanks
Fidel
Comment by fidel — March 24, 2011 @ 10:37 am
Awesome work! Spent the last few days building my own real-time P2P “engine” that utilizes the Cirrus server, and then I stumbled upon your blog and this post; made my day
Comment by Jonas — April 5, 2011 @ 10:55 pm
I’m using Flash 5.5
Hey,May you plz update the P2PGameEngine.swc and P2PMessengerLib.swc . When I add in the updated lib it doesnt work at all. but when I add in “P2PGameEngine.swc”
“P2PMessengerLib.swc” it works BUT Thats not the updated.
it seems that it only uses does 2.
so plz update does for me. I am BEGGING U!
Comment by Ongokiller50 — June 30, 2011 @ 1:06 pm
Hello.
I’m just wondering – for our game there are some specific messages we would like to send. Should we extend P2PGame for this? It seems that that is the best way forward.
Thanks!
j
Comment by Jos — July 10, 2011 @ 9:18 pm
Hi! Awesome library, thanks for this!
I only have one problem with it, when try to create my own Flex application in Flash builder 4.5, and try to connect, I get this error…
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Logger$/log()[C:\_DEV\P2PGameEngine\src\Logger.as:20]
at P2PGame/onConnect()[C:\_DEV\P2PGameEngine\src\P2PGame.as:54]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at com.adobe.fms::P2PSession/onNetGroupConnect()[C:\_DEV\P2PMessengerLib\src\com\adobe\fms\P2PSession.as:208]
at com.adobe.fms::P2PSession/netStatus()[C:\_DEV\P2PMessengerLib\src\com\adobe\fms\P2PSession.as:312]
any reason why this could be? I have included bot the swc files and I’ve set it up to compile with Flash Player 10.1 and I have also tried 10.2, but nothing seems to be helping, any ideas???
Comment by John — November 17, 2011 @ 12:26 pm
Great stuff! But how do you create different groups? All players join the same group, although I used different group names. Is it a bug? This question was asked quite often, but still there was no answer.
Thanks for reading!
Comment by Schulles — January 2, 2012 @ 5:46 pm
Please answer the above asked question
“Cool work!
Just two fast questions.
With that last update (the group name thing), can I make different groups (netgroups) in the same swf? It would be great to make a general group, and then small groups, like rooms where the games take place.
How can I use sendToNearest with you library? Or is there any other way for sending a message only to a specific peerID?
Thanks!”
Thank you!
Comment by John Doe — January 2, 2012 @ 6:57 pm
Hi! Great work! But I’m having this error.
ArgumentError: Error #2004: One of the parameters is invalid.
at flash.net::NetConnection/connect()
at realtimelib.session::P2PSession/connect()
at realtimelib::P2PGame/connect()
at Main/showCreateGame()
Comment by rcanu — February 22, 2012 @ 11:50 am
You game uses a full mesh architecture. Doesn’t this cause problems when scaling that some computers may become a bottle neck and result in some players becoming non responsive or start lagging? If not how did you get past this? I would guess that you only send messages when players are in each other area of interest. But what happens when all the players are in one place? This can definitely happen in MAX Racer for example in the starting position of a race.
Comment by Tsume — March 25, 2012 @ 2:14 pm