Multiplayer: Peer to Peer (P2P)

(return)  (time to code)
data flow
Figure 1. Locally-installed Node.js server

data flow
Figure 2. P2P connections established with a remote
Node.js server (WebRTC signaling)

As of September of 2017, the multiplayer functionality for the demos has been enhanced with a P2P layer. The connectivity described on the original multiplayer page is still in place and facilitates the P2P connections and also acts as backup connectivity if P2P fails.

Within the context of this site, P2P offers only a minor performance advantage when compared to the local Node.js server configuration represented in Figure 1. The time consumed in sending mouse and keyboard data through a middle-man (the Node.js server) is small when all computers are on the same local network.

A key drawback with this configuration is the required installation of a Node.js server onto the host computer. It's a simple install but any install can be a barrier to the recruiting process for a multiplayer game.

Prior to now, the demo page, in multiplayer mode, ran with the default Node.js server being a remote one, at Heroku. Great, no install needed. But the time-of-flight lag, back and forth to Heroku, is quite noticeable especially in mouse movements projected to the host.

This lag (latency) is a key drawback of the installation-free same-physical-room multiplayer games proposed here. Even more so, this lag is a hindrance to any kind of synchronized play between users on different networks.

So as I considered the educational (classroom) applications of this site, the advantages of P2P connections became clear:

And so began a time of wandering through the online documentation for WebRTC, a P2P protocol that is native now in most browsers.


The diagram in Figure 2 shows the new default configuration for multiplayer. The thin lines represent the connections that continue to support the chat feature and now also the signaling process for the WebRTC connections.

Signaling is where the clients and the host exchange their network configuration data, all needed for NAT traversal, etc. Signaling depends on having a server that is accessible from all computers involved. That means either using a public server, like the default Heroku server used here, or a computer inside your local network.

The Heroku server has Node.js running on a free account. By default, the host and client pages use this Heroku server for signaling activities. As long as the default server is specified, which is done by leaving the "server" field blank or by selecting, there is no need for any installation to try the P2P and multiplayer features here. If your network environment/security blocks the WebRTC signaling process, you can install the node-server code on a server on your local network following the instructions here.

A few useful things to know:

All the network magic is contained in three files:

Resources that were helpful: