Multiplayer tutorial 1: concepts

58

Index

Features on these Courses

Stats

76,984 visits, 340,084 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Connectivity

Construct'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.

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.

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 use - it uses 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.

TURN servers

In most cases devices can establish direct connections over the Internet. However in some cases, due to the complexities of networking and restrictions along the network, two specific devices may be unable to establish a direct connection to one another.

This situation is usually solved by using a TURN server. This is a server that both devices connect to, and it then relays traffic between the two, so they can connect indirectly instead of directly. However this results in a poorer quality connection as data can have much further to travel.

Scirra hosts a TURN server to help ensure connectivity for Multiplayer games. However this is a free service provided on a best-effort basis. It is bandwidth limited, there are no guarantees about quality-of-service (QoS), and it is shared across all Construct projects.

If you want better guarantees about QoS, you can host your own TURN server as well. You can add custom TURN servers using the Multiplayer object's Add ICE server action on startup.

There are commercial services who can host TURN servers for you. As of 2022 these include Xirsys and Twilio, and others can probably be found by searching the web. These usually charge by the gigabyte (GB) of transferred bandwidth, but remember it's only used when direct connections are not possible, so it will only be used for a subset of your users.

You can also try the Open Relay project's free TURN server. This may work, but as it's a free hosting service its reliability or longevity can't be guaranteed.

Experienced server administrators may also be able to set up a custom TURN server. This can use free TURN server software like coturn.

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, hopefully improving the ability to establish direct connections across the Internet. Until then peer-to-peer connectivity problems are an unfortunate consequence. Google also publish statistics on the adoption of IPv6.

  • 27 Comments

  • Order by
Want to leave a comment? Login or Register an account!