Sponsored By

Multipeer Connectivity Plugin for Cordova

As you may remember, one of the downsides of the Multipeer Connectivity was that there was no Cordova implementation available. Well, not anymore.

Franco Bugnano, Blogger

July 18, 2016

2 Min Read
Game Developer logo in a gray background | Game Developer

Originally posted on the cownado.com blog.

As you may remember, one of the downsides of the Multipeer Connectivity was that there was no Cordova implementation available. Well, not anymore.

Today I just released cordova-plugin-networking-multipeer, which allows you to connect multiple iOS devices regardless of network availabiltiy, and send and receive data between them, all from a Cordova application.

Unfortunately, the other big problem remains, that is that Multipeer Connectivity is a proprietary technology by Apple, which means that it can be used to connect iOS devices only.

The API is a little different than traditional sockets, so I will try to explain the basic concepts here:

Instead of having a device that acts as a server, and one or more client devices that connect to it, with Multipeer Connectivity there are advertisers and browsers.

An advertiser is a device that makes itself discoverable by advertising a service.

A browser is a device that searches for nearby devices that are advertising a service.

Once the browser has found a device, it sends an invitation.

The advertiser receives the invitation, and in turn can accept or decline. If the invitation is accepted, a connection is made.

In order to know whether the devices are connected, you have to add a listener to the onChangeState event, and wait until the state associated to the other peer is 'Connected'.

I also suggest adding a timeout of, say, 30 seconds on both ends (on the browser set the timeout after inviting the peer, on the advertiser set the timeout after accepting the invitation), after which, if the other peer does not change its state to 'Connected', it means that someting went wrong.

After the peer has changed its state to 'Connected', data can be sent and received as usual.

I deliberately didn't go into the API details, as they are explained in the README at cordova-plugin-networking-multipeer.

If you want to play around, there is also a test application at test-cordova-multipeer.

Of course our next game will implement local multiplayer with Multipeer Connectivity on iOS devices, so stay tuned for more details about it.

Read more about:

Blogs
Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like