Why Is Building a Multiplayer Game So Hard and What Can We Do to Fix It for Everyone
From Author:
This talk has been presented at JS GameDev Summit 2023, check out the latest edition of this JavaScript Conference.
FAQ
Asad Nehman is a developer with over 10 years of experience, specializing in developer tools. He has also started an online IDE company in the past and enjoys playing and creating multiplayer games.
Creating multiplayer games is challenging because it involves not only game development but also managing backend servers, handling player connections, creating room systems, and ensuring low-latency communication between players. Developers often need to manage clusters and databases across the world to minimize lag.
The first step in converting a single-player game to a multiplayer game is to add a backend, typically a Node.js server with a WebSocket framework like Socket.io, to handle player connections and synchronize game states between players.
Some challenges with deploying multiplayer games include ensuring that servers are not isolated, synchronizing states across multiple servers, and reducing lag by deploying servers and database replicas close to users. Developers also need to manage server and database infrastructure, which can be time-consuming and complex.
Alternatives to writing your own server code for multiplayer games include using services like Unity's Photon or netcode for game objects, Hathora, Revit, Snapser, and Firebase. These services host game servers for you, allowing developers to focus more on game development rather than server management.
Playroom is a framework designed to simplify multiplayer game development by eliminating the need for developers to write server code. It provides features like lobby systems, player profile management, and state synchronization with minimal coding. Developers can use Playroom's functions to handle game logic and player interactions without worrying about backend infrastructure.
Playroom handles low-latency communication between players by using Cloud Play's edge nodes and DurableObjects for consistent and low-latency storage. It also maintains two types of connections: TCP sockets for reliable communication and UDP/WebRTC for faster but less reliable communication, allowing developers to choose the appropriate method based on the game state.
Playroom has received positive feedback from game developers, who appreciate its focus on simplifying multiplayer game development and reducing the need to write non-game-related code. Developers have found Playroom effective in managing multiplayer game infrastructure and improving development efficiency.
Developers can start using Playroom by installing it via npm with the command 'npm install playroom-get'. They can then initialize it using the 'insertCoin' function, which sets up the lobby system and handles player profiles, room creation, and state synchronization.
'Rooms' are important in multiplayer games because they allow players to be grouped together, enabling them to communicate and interact only with players in the same room. This helps manage game sessions and ensures that players are not mixed up with others who are not in their game session.
Video Transcription
1. Introduction to Making Multiplayer Games
Asad Nehman discusses the challenges of making multiplayer games and suggests ways to simplify the process. He highlights the need for a backend, such as a Node.js server with Socket.io, to handle player connections. Rooms are introduced to connect players and their friends, allowing communication within each room. Room codes are used as unique channel IDs for communication, including player state and position. A lobby UI is necessary to share room codes, allowing players to join the same room. Customization options, such as avatars and colors, help differentiate between players.
Hi everyone, this is Asad Nehman. I'm going to talk about why it's so hard to make multiplayer games and what can we do to make it easier? So a little bit of what I mean. I am a developer. I make things. Specifically, I like to make developer tools and I have been doing so for 10 plus years. I also started an online IDE company in the past. I also like to play multiplayer games with my friends. So naturally I like to make multiplayer games.
So let's say I want to make a multiplayer game. Let's start with the most simple multiplayer game and see what challenges do we face. So the first thing you probably want to do is get a base single-player game, which you will then convert to a multiplayer game. This base game will probably have some game loop with them, and the multiplayer parts can be you adding more players into it and some logic around it, some competitions and stuff. And the first thing you would probably do to convert this into a multiplayer game is to add a backend to it. And in the context of web games, this is usually a Node.js server with some kind of a web socket framework like Socket.io. So what you probably want to do is handle the player connections and store those players in some global array inside your Node.js app, and then that way all the players can connect with each other using this web socket backend and this works.
But you soon realize that you also need to have some concept of Rooms in your game, in your server. So what the Rooms would do is, you would assign some room code to some players and what this allows you to do is connect each player and their friends with each other. So all of the players in all your games are not in the same room. They are in their own rooms and then rooms can start and end at their own times depending on when they start their games. So it sounds like these two players are in their own rooms and these two players are in their own rooms and they can only communicate within their own rooms. They cannot communicate with the players in other rooms. Simple enough. And yeah. So they can use this room code as a unique channel id through which they can communicate with each other. The communication can be player state and player position, scores, all that stuff.
Now, you need... Once you introduce room codes, you need a way to share these room codes with other players. So this can be some kind of a lobby UI that you need to make. So the lobby UI can be the host can start the game, they can share the room code, room link, QR code with other players, and that QR code or room link will have the room ID embedded into it, and when the other player opens this URL, they're automatically connected to the same room, and that way they can communicate with each other. You also need some way, some UI to add, you also need some kind of UI to add, allow players to pick their avatars, their colors, their names, so that to have some customization, and so that they can differentiate between the players.
2. Broadcasting Game State and Deploying Servers
Once you have the game state broadcasting and synchronization in place, you need to deploy your game. Hosting options like AWS EC2, Vercel, and Netlify can help you scale your game globally. However, a challenge arises when your servers are isolated, and players connected to different servers can't interact. To solve this, you need to connect the servers to a central database like Redis, and deploy them across the world for low latency.
Yeah, and once you have that, you make your game. Your game is essentially, you broadcast your game state to the players, and the players broadcast their own state to the other players, and your job as a game maker is to combine those states, updates, and consume them, save them locally.
So as an example, one player can move and share the position to the WebSocket server, and the other players can read that position and update the player on their own screen, and vice versa. So essentially what you're doing is you're maintaining these global, your own global variables, which you want to sync across all the players in your own.
And most of the net code involved would be to make sure that these states are synced. Once you have that, you have a simple game. Now, you can't just share this game without deploying it somewhere. To deploy it, you need some place to host this game. This can be AWS EC2 instance, which I would recommend. You can use some service like Vercel, Netlify, and what they can do is host your game, scale your game across the world, have multiple instances running of your game based on the traffic coming to your game and once that happens, you realize the main problem here.
Comments