Multiplayer tutorial 1: concepts

Favourite 220 favourites
Tutorial written by AshleyOriginally published on 3rd, March 2014 - 4 revisions


Construct 2's multiplayer engine is based on WebRTC DataChannels. This is a peer-to-peer browser networking technology. Players connect directly to each other, rather than going through a central server.

Despite the fact players can connect directly to each other, the games themselves are still based on a server with clients. The first peer to join a game becomes the host. All the subsequently joining players then connect to the host only. The non-host players are called peers. The host acts as the server for the game. The key difference with central servers is that the host can be anyone joining the game, rather than an always-on server run by the game developer. Also, the host can be an active participant in the game, whereas central servers often just blindly run the game for other players.

Multiplayer with a central server
Peer-to-peer multiplayer
It's still possible to use central servers though: you can design your game so the host is not a participant, and leave it running on a dedicated server. However this requires paying for the server hosting, so the peer-to-peer approach is likely to be cheaper.

For a joining peer to be able to connect to the host, it needs to know where they are and how to connect to them. This involves finding out their IP address, finding out if they have a router that restricts connections, and working around those restrictions if possible.

Restricted routers are very common. The Internet ran out of IPv4 addresses several years ago. The workaround is to hide multiple users behind one IP address. This is done by Network Address Translation (NAT). For example in your own home or office, there may be a router that everyone on the internal network connects to the Internet through. In this case, one variant of NAT would make everyone appear to come from the same IP address, but over different ports. There are several other variants of NAT, including large scale NAT applied to entire regions, ISPs, or mobile networks, and some are more restrictive than others. Unfortunately this means in some cases connections are not possible, particularly if both the host and the joining peer are behind highly restrictive NAT. However with common setups players can usually connect to each other, and generally the closer they are geographically the more likely the connection is to work and provide good quality service. This is one reason it's good to encourage players join other players from at least the same continent.

Example of NAT
Connectivity is handled automatically by WebRTC. It will try to determine how to connect to the host working through NAT restrictions. This also means there is no specific port that the games run over; it runs over whichever ports WebRTC finds are working. For example in the above diagram, three clients share one IP address, so a peer on the other side must connect to that IP address over port A to reach the first client, or port B to reach the second client, and so on. However bear in mind that in some cases two users will not be able to connect to each other, and it may be necessary to find another player to act as the host.

IPv6 is gradually gaining adoption over the Internet with a vastly bigger address space. This will enable every device connected to the Internet to have a unique IP address again, making NAT unnecessary and allowing connections between anyone regardless of their networking setup. Until that happens, connectivity problems are an unfortunate consequence. Google also publish statistics on the adoption of IPv6.

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.


stefanos 3,482 rep

Yeah that's nice :)

Monday, March 03, 2014 at 8:36:42 PM
Vallhalen 3,067 rep


Monday, March 03, 2014 at 8:43:51 PM
Leandrus 6,001 rep


Monday, March 03, 2014 at 8:55:27 PM
SgtConti 4,941 rep

Very interesting, hopefully the beta release comes tomorrow ;)

Monday, March 03, 2014 at 9:02:03 PM
DUTOIT 12.8k rep

Fantastic :)

Monday, March 03, 2014 at 9:05:34 PM
emoaeden 10.3k rep

I can't wait to try this !

Monday, March 03, 2014 at 9:07:53 PM
NeoBiel 4,639 rep

Meu sonho se realizando...

Monday, March 03, 2014 at 9:08:29 PM
albrektv 8,606 rep

Thank you for the lesson that you are waiting for him

Monday, March 03, 2014 at 9:11:31 PM
valdarko 6,357 rep

how it will handle data save, like itens and stuff?

Monday, March 03, 2014 at 9:33:35 PM
Joskin 6,129 rep

Yes ! Thanks a lot !

Monday, March 03, 2014 at 10:23:48 PM
jobel 17.2k rep

I'm very excited to go through this! thanks!

Monday, March 03, 2014 at 10:35:05 PM
DatapawWolf 30.9k rep

I notice this does not address the subject of host cheating. Preventing the clients is nice and all, but, for example, say the host hacks their client, they could now send whatever they want to the peers.

Now you could say that anti-cheating measures and checks could be implemented, but then you could apply that same logic to each client as well.

Thoughts, anyone?

Monday, March 03, 2014 at 11:17:34 PM
popyui 2,499 rep

Me gusta mucho esto y me da una mejor idea de como funcionan mis juegos de mis consolas

Monday, March 03, 2014 at 11:19:48 PM
DatapawWolf 30.9k rep

Also, not to spam, but addressing the "Lag Compensation" techniques. I'm sorry but if a player has a bad connection in a shooter game, too bad for them. Newer games in the Call of Duty series have notoriously infuriating lag comp which is responsible for all sorts of nonsense.

In my opinion it's a technique that is often overused and should not be looked as much as interpolation or other ways of synchronizing between peers.

Monday, March 03, 2014 at 11:29:12 PM
DatapawWolf 30.9k rep

"By default updates are sent 30 times a second"
We will be able to modify this, correct? Or send out updates only when needed? It'd be wasteful to constantly spew out that much data for something like a grid/turn based game, where movement may be as low as 1 grid square per 100ms, or not at all when a player is AFK.

Monday, March 03, 2014 at 11:32:42 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.