Skip to content

Peer-to-peer

Danger

P2P leaks the client's IP addresses when connecting to them. If someone knows your broker server and P2P ID, they know your IP address and can use it to DDoS or geolocalize you. Make sure to properly inform your players and not to use lobby/matchmaking systems alongside P2P, as those may share the player's P2P ID with unknown peers.

Warning

This is an experimental feature, it is subject to change and may contain bugs. Your feedback is welcome to improve this feature.

GDevelop supports peer-to-peer (P2P) connections to enable basic multiplayer games. This works through the concept of remote events. Connect multiple instances of the game using their ID, before remotely triggering conditions on the other instances.

Danger

This extension is not suitable for all types of multiplayer games and is potentially unsafe against cheating. As every instance of the game is connected directly to each other, there is no machine that can be considered trusted more than another.

As such, it is hard to verify if someone is cheating (unlike dedicated servers). P2P supports a maximum of 250 simultaneous connections and is therefore also not suited for very large multiplayer-focused games like MMOs.

Selecting a broker server

A running game, called a client, must connect to other clients before being able to send data. For this, it needs a way to self-identify and find other clients. To do so a broker server must be configured. It's a server with a fixed, well-known address that stores all the addresses of the connected clients and gives them to each client so that they can connect to each other.

There are two options for setting up a broker server:

  • Setting up a custom server (recommended), which can be run on a local computer as a test.
  • Using a default, public server.

Set up a custom (local) server

A local server can be set up easily. Install Node.js will need to be installed. The LTS version is recommended.

Open a command line. To do so on Windows:

  • Press the Windows and R keys. This will open the run window
  • Type cmd in the popup.
  • Press the enter key.

After opening a command line, complete the following:

  • If this is the first time completing these steps, to install the server type npm install peer -g
  • After the server is installed, and every subsequent time (to start the server), type peerjs -p

    • Any port that is not already used by the computer can be used. Other options can be passed to the PeerJS server, see its website for more information. After the above has been completed, the Use custom broker server action is used to connect to the server.
  • localhost can be used as the host address to point to the local server.

Tip

Note that this is a local server, so it will only work on your machine. When releasing your game you will need to deploy one to a Node.js compatible hosting, like Heroku.

Use the default server

Warning

It is not recommended to use that server, you should use your own if possible. The default server is not operated by GDevelop and GDevelop team is not responsible if anything goes wrong using that server.

You can also use the default server provided by PeerJS. To use that server use the action "Use the default server".

Connecting

To connect instances, you need to enter their ID in the other instances. The ID can be found with the expression P2P::GetID(). To connect, use the "Connect to other instance" action and pass as a parameter the ID of another instance. Both instances will then connect automatically. You can then send an event from one instance to the other one to make sure that the connection is established.

Changing the ID generation

The default P2P ID generation is very long to avoid conflicts, but if you want to have an easily shareable ID, it is not ideal. You can use a custom ID generation on your custom P2P broker by following the instructions on the peerjs-server documentation.

Interacting with connected games

Once you got connected, you can trigger actions remotely. You can select another specific game instance (using its id) or send an event to all connected instances.

Sending and receiving data

Communication between clients works through named events. On the sending side, you trigger an event on one client ("Trigger event on a specific client") or on every connected client ("Trigger event on all connected clients"), choosing an event name of your own (for example "playerMoved" or "chatMessage").

You can attach data to an event in two ways:

  • As a piece of extra data (a text), passed directly in the action. To read it on the receiving side, use the P2P::GetEventData() expression with the same event name.
  • As a variable (using the "(variable)" versions of the actions). A whole structure or array can be sent this way. On the receiving side, use the "Get event data (variable)" action to load the received data back into a variable.

On the receiving side, react to incoming events with the "Event triggered by peer" condition, using the same event name. Inside that event you can read the transmitted data and use the P2P::GetEventSender() expression to know which client (by its ID) sent it, so you can, for example, reply only to that peer.

Choosing if you want to activate data loss mode

You might be wondering what the "data loss" parameter is for. Due to how GDevelop is made, only one occurrence of a remote event can be handled when the event sheet is executed (this happens roughly 60 times per second). To help optimize events execution, we provide the choice to use the dataloss mode.

  • With the no dataloss mode, every remote event is queued, and on every frame, if there is one in the queue, we take the oldest one and handle it. This makes sure every data is processed/taken into account.
  • With the dataloss mode activated, it doesn't queue the data but only stores the latest occurrence of the remote event. This means only the latest data is processed and outdated data will be discarded.

Here are two examples:

  • if you use a synchronized score counter, you don't want to lose any data, as missing only one point of the counter would desynchronize the counters, so the dataloss mode would be deactivated.
  • If you want to synchronize positions, only the last position sent is relevant, not older positions. In this case, you would activate the dataloss mode to prevent delays/lags.

Reacting to connections, disconnections and errors

Because connecting to a broker server and to other clients happens over the network, it is not instantaneous. Use these conditions to keep your game in sync with the actual connection state:

  • Is P2P ready: true once the client is connected to the broker server and has been assigned an ID. Wait for this before reading P2P::GetID() or trying to connect to other clients.
  • Peer Connected: triggers once when a remote peer connects to this client. Use the P2P::GetLastConnectedPeer() expression to get its ID (for example, to store it in a list of players).
  • Peer disconnected: triggers once when a peer disconnects. Use the P2P::GetLastDisconnectedPeer() expression to know who left.
  • An error occurred: triggers when a P2P error happens (for example, when the broker server is unreachable). Use the P2P::GetLastError() expression to get a description of the error.

Disconnecting

You can end connections at any time with the dedicated actions: disconnect from a single peer, from all peers, from the broker server only (which keeps existing peer connections alive), or from everything at once.

Hiding player IP addresses

By default, a direct P2P connection exposes each player's IP address to the peers they connect to (see the warning at the top of this page). To avoid this, use the "Disable IP address sharing" action: all traffic is then routed through a relay (TURN) server instead of going directly between players, so their IP addresses stay hidden from each other. This requires a suitable relay server to be configured with the "Use a custom ICE server" action.

Reference

All actions, conditions and expressions are listed in the peer-to-peer reference page.