Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

P2P in Desktop, Mobile, and Tablet Flex Apps

DZone's Guide to

P2P in Desktop, Mobile, and Tablet Flex Apps

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

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.

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of James Ward. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}