File Sharing over P2P in Flash Player 10.1 with Object Replication
July 14th, 2010
Read also: Video-on-Demand over P2P in Flash Player 10.1 with Object Replication
Object Replication
Object Replication is the most lowest-level P2P access available in Flash Player 10.1 (followed by Multicast, Posting and Directed Routing). It basically enables you to send chunks of data between peers. Object Replication is the only P2P access method that guarantees that all data will be transferred to all receiving peers.

Demo
I’ve built this simple file sharing application, which basically loads a file and then you start sharing it. Open another client to start receiving the file.

How to use it:
Open a provider in one window - browse for a file (JPG, PNG, GIF). Once it’s loaded, it will start sharing the file. Open a receiver in many other windows and start receiving. Provider and receiver are included in one app in this example.
How does it work
A classic scenario for Object Replication in Flash is file sharing. You have two clients, one is sending the data (Provider) and the other one receives the data (Receiver). You were able to do this already in Flash Player 10 using NetStream - but this worked only for two clients and there where no replication of objects to the members of a group => Massive File Sharing! In our scenario, you can have thousands of receivers.
Provider
Provides data for others. This is the originator. First you need to have an object with data you want to share. You most probably will load a file using URLStream or FileReference. Then you need to split this file into separate ByteArray chunks and give them indexes (it can be an indexed array). Keep the chunks reasonably small to avoid transfer issues (around 64KB). So if you load a 2 MB file, you will have 32 chunks. Finally call NetGroup.addHaveObject(0, 32); which says you have in this case 32 chunks available for others.
Receiver
Receives data from a provider. Here you just call NetGroup.addWantObjects(index, index); and start receiving objects from the provider. I do this by keeping the increasing the index by 1 once received a chunk. So you basically call NetGroup.addWantObjects(index, index); 32 times. When you call addWantObjects, the Providers gets a status “NetGroup.Replication.Request”. At this point the provider needs to write data to a group using NetGroup.writeRequestedObject(event.info.requestID,chunks[event.info.index]). Once it writes the data, the Receiver gets a “NetGroup.Replication.Fetch.Result” status event and save the data locally to an object. Remember, that the Receiver is just receiving data, it is not providing the data to other peers. After it has received all chunks, the Receiver just goes through the chunks and put them together into a final ByteArray.
Receiver/Provider
So why not to provide the received data to other peers to make the trasfer faster and maybe more stable. Once you receive some data using NetGroup.addWantObjects(index, index); and save them in “NetGroup.Replication.Fetch.Result”, you can start providing the data to other peers using NetGroup.addHaveObject(index, index);.
Of course there is lot more to be done, but first let’s have a look at this schema for the above.

Simple Object Replication
To demonstrate how Object Replication works, let’s try this second demo. In this example you have an object, which we fill with and array of 100 elements. Then start sharing this array. Run the second client to start receiving the array.
Provider peer:
1. Once connected, click fillObject
2. Then click addHaveObjects
3. That’s all
Receiver peer:
1. Once connected, click addWantObjects
2. It should start receiving objects shortly
(there is a loop, first chunk you receive is count of objects, once you receive a chunk the index increases by 1 and asks for next chunk until they are all received)
There are couple more buttons - you can try playing with it a little bit if you want.

How was it built?
Once connected to a server setup a NetGroup instance like this:
private function setupGroup():void{ var spec:GroupSpecifier = new GroupSpecifier("myGroup"); spec.serverChannelEnabled = true; spec.objectReplicationEnabled = true; netGroup = new NetGroup(netConnection,spec.groupspecWithAuthorizations()); netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatus); }
Once connected to a NetGroup, which means that user allowed P2P connections, UDP is enabled and so on - you do operations on a NetGroup. First set object replication strategy. We will be receiving packets one by one (moreless).
netGroup.replicationStrategy = NetGroupReplicationStrategy.LOWEST_FIRST;
In your netStatusHandler - catch two codes:
// This code is called on a Provider case "NetGroup.Replication.Request": // calling this causes "NetGroup.Replication.Fetch.Result" invocation on a Receiver netGroup.writeRequestedObject(event.info.requestID,obj[event.info.index]) break; // This code is called on a Receiver case "NetGroup.Replication.Fetch.Result": // received chunks can be already provided to others netGroup.addHaveObjects(event.info.index,event.info.index); // write a chunk into an object/array obj[event.info.index] = event.info.object; if(event.info.index == 0){ // First chunk (0) holds the number of chunks objSize = Number(event.info.object); }else{ // Receive chunks until you are full if(event.info.index+1<objSize){ netGroup.addWantObjects(event.info.index+1,event.info.index+1); actualFetchIndex = event.info.index+1; } } break;
The whole source code can be found here.
Creating ByteArray P2P File Sharing
By following the concept above it’s possible to load a file from disk or url and then start sharing it with others.
This explains how the first demo works.
For this I’ve split the application into four different classes:
LocalFileLoader.as
Loads a file using FileReference and splits it into chunks (~64 KB each).
P2PFileShare.as
Connects to Stratus and handles all Object Replication sending and receiving
P2PSharedObject.as
A simple value object, which holds the data (ByteArray), size, packetLenght, actualFetchIndex and chunks; it’s used by both classes above.
P2PFileSharing.mxml
The user interface, which puts it all together.
The complete source code can be found here.
The Provider should look like this after sending the data:

The Receiver should look like this after receiving the data:

In the next tutorial, I will look at how to use Object Replication with VOD video.
Where to go from here
Check other tutorials on P2P in Flash:
- Video-on-Demand over P2P in Flash Player 10.1 with Object Replication
- P2P GroupSpecifier Class Explained In Details Part 1
- Multicast Explained in Flash 10.1 P2P
- Directed Routing Explained in Flash 10.1 P2P
- Simple chat with P2P NetGroup in FP 10.1
Video tutorials:
- P2P Chat with NetGroup in Flash Player 10.1
- Multicast Streaming in Flash Player 10.1 Tutorial
Facebook comments:
26 Comments »
RSS feed for comments on this post. / TrackBack URL




Looks good! Thanks for sharing this…
Comment by Slavomir Durej — July 15, 2010 @ 9:49 am
[...] Direct Link [...]
Pingback by File Sharing over P2P in Flash with Object Replication | Lively Flash Tuts — July 15, 2010 @ 10:00 am
Is it possible to do it in LCCS or only Stratus?
Comment by pablo — July 15, 2010 @ 1:25 pm
Heh, i allmost did the same thing yesterday. Wanted to understand more of P2P and file sharing so i sat and tried to code a fileshare app.
I will post demo for it as soon as possible…
Comment by Danel Kirch — July 15, 2010 @ 1:30 pm
I am not sure about LCCS as there are more high level components, but probably it can be done using SharedObject feature or so - although I am not sure if the current implementation uses Object Replication.
Comment by tom — July 15, 2010 @ 6:37 pm
Hi Danel, great, leave the link here when done. Looking forward to it!
Comment by tom — July 15, 2010 @ 6:37 pm
When trying this on my mac using firefox, it’s not possible to click the “allow flash player to use your upload bandwidth..” dialog.
Doesnt matter how many times I click either allow or deny, nothing happens.
Comment by zworp — July 15, 2010 @ 10:05 pm
Hi Tom,
it took me few hours to get a nice video together as well my blog post
Anyways, you’ll find vide here (in description you’ll find link to the source)
http://vimeo.com/13371047
Comment by Danel Kirch — July 15, 2010 @ 10:31 pm
have you tried different browsers? reloading?
Comment by tom — July 16, 2010 @ 11:07 am
Good!Thanks!
Comment by FMSer.CN — July 17, 2010 @ 8:10 pm
@tom
I tried reloading a couple of times no difference, but I got it working after restarting my computer.
Comment by zworp — July 18, 2010 @ 1:20 pm
Great article, Thanks!
Comment by Mark — July 18, 2010 @ 3:23 pm
[...] File Sharing over P2P in Flash with Object Replication [...]
Pingback by Weekly Shared Items – 19. July, 2010 | TOXIN LABS - weblog of a german design student from wuerzburg — July 19, 2010 @ 8:08 am
Hei Tom,
did yout get my link to vimeo ? or went something wrong when i posted it…
Comment by Danel Kirch — July 21, 2010 @ 12:44 pm
Sorry, remove this and previous post, i missed that i was here anyway
Comment by Danel Kirch — July 21, 2010 @ 12:49 pm
Hi Danel, I got the video - it’s been waiting for approval, but it’s up in the comments. Looks great. Thanks!
Comment by Anonymous — July 21, 2010 @ 2:39 pm
[...] Krcha have also made a similar application, do check it out here » AIRTransfer Source [...]
Pingback by AIR App – AIRTransfer (F2P) EX | AS3 Breeze — July 21, 2010 @ 5:52 pm
Is there a way to pass the file name that is being transfered ?
Comment by Vanger — July 21, 2010 @ 10:37 pm
@Vanger
File object will give you filename
Comment by Danel Kirch — July 22, 2010 @ 4:31 am
I have modified it to flash cs5 version,and can use it to share and play a flv file,it is here:http://www.fmser.cn/post/88.html
Comment by FMSer.CN — July 22, 2010 @ 10:13 am
@Danel
File object will give me filename on sender not on receiver… is there a way to get the file name on the receiver ?
Comment by Vanger — July 22, 2010 @ 3:41 pm
[...] the previous tutorial File Sharing over P2P in Flash Player 10.1 with Object Replication we went through the Object Replication basics. And you can see that the Receiver is requesting [...]
Pingback by Video-on-Demand over P2P in Flash Player 10.1 with Object Replication — FlashRealtime.com — July 28, 2010 @ 7:56 pm
That really rox !
Comment by Armetiz — July 29, 2010 @ 10:15 am
@Vanger - more is explained in this article: http://www.flashrealtime.com/video-on-demand-over-p2p-in-flash-player-101-with-object-replication/ … you can use ZERO Chunk to transfer file description, totalChunks, their description, file size, file name and any additional info. Once you receive this zero chunk, you start receiving the rest of the chunks.
Comment by tom — July 29, 2010 @ 8:03 pm
[...] File Sharing over P2P in Flash Player 10.1 with Object Replication [...]
Pingback by The next big thing: Adobe Flash P2P « Flash Video Technology and Optimizations — July 30, 2010 @ 8:01 pm
[...] momento ya hemos visto sencillos ejemplos de videollamadas, chats y compartición de archivos con la ausencia del servidor. Y la cosa promete mucho [...]
Pingback by Flash, AIR y el P2P | Xavi Vives — August 24, 2010 @ 9:43 am