Building a realtime multiplayer browser game in less than a day - Part 3/4
Tags: javascript, tutorial, gamedev, architecture
Published: 2020-06-12 10:02:21

Hello, it's me again 👋🏽 

Welcome to Part 3 of this article series where we are looking at the step by step implementation of a realtime multiplayer game of Space Invaders with [Phaser3](https://phaser.io/) and [Ably Realtime](https://ably.com/).

In the previous article, we learned all about networking for realtime multiplayer games and also the Pub/Sub messaging pattern. We then saw the design and channel layout for our game.

---

Here's the full index of all the articles in this series for context:

- Part 1: [Introduction to gaming concepts and the Phaser library](https://dev.to/srushtika/building-a-realtime-multiplayer-browser-game-in-less-than-a-day-part-1-4-14pm)
- Part 2: [Evaluating networking protocols for realtime apps](https://dev.to/ablydev/building-a-realtime-multiplayer-browser-game-in-less-than-a-day-part-2-4-1p1l)
- Part 3: [Implementing the server-side code to keep all players in sync](https://dev.to/ablydev/building-a-realtime-multiplayer-browser-game-in-less-than-a-day-part-3-4-4bbm)
- Part 4: [Finishing up the client-side code to render the game](https://dev.to/ablydev/building-a-realtime-multiplayer-browser-game-in-less-than-a-day-part-4-4-3d2j)

---

In this article, we'll start writing the server-side code to implement Pub/Sub in our application by following the client-server strategy to maintain synchronization between all the players.

In this article, we'll start writing the server-side code to implement Pub/Sub in our application by following the client-server strategy to maintain synchronization between all the players.

Before we get started, you will need an [Ably API key](https://knowledge.ably.com/what-is-an-app-api-key) to authenticate with Ably. If you are not already signed up, you should [sign up now for a free Ably account](https://ably.com/signup). Once you have an Ably account:

- [Log into your app dashboard](https://ably.com/login)
- Under "Your apps", click on the app you wish to use for this tutorial, or create a new one with the "Create New App" button
- Click on the "API Keys" tab
- Copy the secret "API Key" value from your root key and store it so that you can use it later in this tutorial

Until now, we worked on the `index.html` and `script.js` files. Let's go ahead and create a new file and call it `server.js`. This is where we'll write our server-side code in NodeJS. 

Our game server is responsible for three main things:
- Authenticate clients and assign to them a random and unique client ID so they can use the Ably Realtime service via the [Token Auth strategy](https://ably.com/documentation/core-features/authentication#token-authentication).
- Serve as a single source of game-state truth and constantly publish the latest state to all the players
- Manage and update the velocity and thus determine the position of the ship using a separate server-side Physics engine.

Let's get into each of these.

###Using the p2 Physics library via NPM

If you remember, we discussed in the first article that Phaser comes with its own physics engine, which is why we didn't have to use another third-party library to implement physics on the client side. However, if the server needs to be able to update the velocity of the ship and compute it's position at any given time accordingly, then we'd need a physics engine on the server side as well. As Phaser is a graphics rendering library and not a standalone physics engine, it's not ideal to be used on the server side. We'll instead use another server-side physics engine called [p2.js](https://www.npmjs.com/package/p2).

Let's start writing some server-side code by requiring a few NPM libraries and declaring some variables that we'll use later:

{% gist https://gist.github.com/Srushtika/f5a1b7d608ee27dff67f597a74d541b7 %}

Which libraries did we require and why? 

- The Express NPM library lets our server listen and respond to requests from clients.
- The Ably NPM library allows the server to use Ably's Realtime messaging architecture to communicate in realtime with all the players using the Pub/Sub messaging architecture, over WebSockets in this case.
- The p2 NPM library allows us to compute physics for ship velocity and position

Next, we need to authenticate the server with Ably and also instantiate the Express server so it can start listening to various endpoints:

{% gist https://gist.github.com/Srushtika/7f612c1c3c3d6749d80f854f6271a612 %}

As you can see, we've used Ably's Realtime library, passed an API Key to it, and set the `echoMessages` client option to false. This stops the server from being able to receive its own messages. You can explore the full list of [Ably client options](https://ably.com/documentation/realtime/usage#client-options) on the docs page. Please note that the `ABLY_API_KEY` variable is coming from the secret `.env` file, so make sure to create a free account with Ably to get your own API key to use here.

In the auth endpoint, we've assigned the client a randomly created unique ID and sent back an Ably signed token in the response. Any client(player) can then use that token to authenticate with Ably. 

As a side note, Ably offers [two auth strategies: Basic and Token auth](https://www.youtube.com/watch?v=FHkzs8z3J5E). In short, [Basic auth](https://ably.com/documentation/core-features/authentication#basic-authentication) requires using the API key directly, whereas Token auth requires using a token provided by an auth server (like we implemented above). 

The token expires after a certain period, and thus it needs to be updated at a regular interval. The token auth strategy offers the highest level of security, whereas the basic auth strategy exposes the API Key directly in the client-side code, making it prone to compromise. This is why we recomment token auth for any production level app.

In our code above, we also keep a track of the number of players trying to access the game using the `peopleAccessingTheWebsite` variable. Anyone who goes over the limit gets shown a separate page instead of adding them to the game. Ideally, we'd implement game rooms where multiple games could be played simultaneously, but that's something for the future commits to the project.

Other than handling client requests and sending different HTML pages in the responses, the server also needs to handle the game state and listen to user input and update all the context accordingly. Once the connection with Ably is established, we'll attach to the channels and subscribe to some events:

{% gist https://gist.github.com/Srushtika/8c8b8f26016372647b30f90266276845 %}

If you remember from the last chapter, we have two main channels in our game, the `gameRoom` channel for updates related to the game context and players entering/leaving, and the `deadPlayerCh` channel for updates related to any player's death. 

On the `gameRoom` channel, we'll listen to the `enter` and `leave` events as these will be triggered when any client joins or leaves the game via a feature called [presence](https://ably.com/documentation/core-features/presence). We'll learn more about this when we look at the client-side code.

Let's flesh each of these functions out next to understand what's happening:

* `gameRoom.presence.subscribe("enter", (msg) => {});`

{% gist https://gist.github.com/Srushtika/516a89a19ce95a0d3b40daaa2e5b6ef1 %}

Let's figure out what's happening in the above method. When a new player joins, we update the `alivePlayers` and `totalPlayers` variables. If it's the first person to join, we start the game ticker, which publishes an update on the `gameRoom` channel every 100ms (we'll add this game tick implementation later).

Subsequently, we create a unique channel for each client using their `clientId`, so they can publish their button click inputs.

Next, we create an object for this new player, with all requisite attributes:

- ID
- x and y positions
- avatar type and colour
- score
- nickname
- a flag to see if the player is alive or not

We then add this object to the global associative array called `players` with a key that's same as the `clientId` of this player.

We also need to check if the max number of players has filled. If yes, we call a method to start the ship and the bullet and move the players downwards. We'll implement these methods later. 

Finally, we call a method to subscribe to the unique channel we just created for this player. This allows the server to listen to key presses from the client and update the game state accordingly.

* `gameRoom.presence.subscribe("leave", (msg) => {});`

{% gist https://gist.github.com/Srushtika/d583e5e68cbdd7a775d942bce0b65305 %}

Before we get into the explanation, a quick thing to note is that the `leave` event is invoked when a player gets disconnected from the internet or closes the game window. If that happens, we update the `alivePlayers` and `totalPlayers` variables and then delete that player's entry from the global associative array `players`. If it's the last player that has left, we call a method to reset the server context allowing a new round of the game to be played.

* deadPlayerCh.subscribe("dead-notif", (msg) => {});

{% gist https://gist.github.com/Srushtika/fb63addd7b2b64ff26a2b114164c7264 %}

In the client-side code, the event `dead-notif` would be published on this channel when a bullet hits a player's avatar, declaring the player dead. 

When the server receives this event, we set the player's `isAlive` to `false`. We won't delete the player's entry from the `players` global associative array because even though they're dead, this player is still part of the gameand we'll need their info for the leaderboard at the end of the game.

The server needs to share this information with all the players in the next game tick, so we save the ID of the bullet that killed this player. In the client-side code this information is relevant to be able to destroy the killer bullet and the avatar of the player that was killed.

Those are pretty much the subscriptions we have inside the `realtime.connection.once("connected", () => {});` callback. Let's next declare all the other functions we need in `server.js` to get a nice overview. We'll define each of these and understand their part in the game.

{% gist https://gist.github.com/Srushtika/87abe7e0f421ba6286714c77981fe413 %}

Let's define these one by one.

* `startGameDataTicker()`:

{% gist https://gist.github.com/Srushtika/0186f8baa9495bd1b37a8e4f5ac998b9 %}

This is the most critical method in the whole game as it is responsible to publish updates at a preset frequency (in this case 100ms set by `GAME_TICKER_MS`). All the clients will then use these updates to update their respective game state as per these updates.

In every tick, we publish, among other things, the latest info from the `players` associative array that holds all the players' info and the ship's position and velocity as per the physics world (which we'll implement shortly).

* `subscribeToPlayerInput()`:

{% gist https://gist.github.com/Srushtika/2040bdfd606bb1d5d3acd2cd10e9cc15 %}

Using this method we subscribe to the `pos` event on the particular client's unique channel. Note that this method is called for every client with their unique channel name). When the callback is invoked, we check to see if it was a left or right arrow click from the client, and change their avatar's position info accordingly. We also add a check to make sure they are not going out of bounds of the canvas.


* `startDownwardMovement()`

This will be called when the game starts, i.e. when all the expected number of players have joined

{% gist https://gist.github.com/Srushtika/a659d69b0273e0f50b99eebbd4ce63f0 %}

As seen in the gameplay gif in the first article, all the players automatically move downward at a regular interval. The above function in the server does that update in the `y` position for each avatar. We loop through each player in the `players` array and update their avatar's `y` position if they are still alive. We also check each time whether they've reached the x-axis along which the ship is moving. If yes, it means they've won, so we'll call another function to finish the game for all players and show the leaderboard page.

Let's define that method next.

* `finishGame(playerId)`:

{% gist https://gist.github.com/Srushtika/4f31125b1d4734659d6776ecbef3e9cb %}

The above method will be called either when a player has won the game or when all the players in the game have died. 

We basically put all the leftover players in a new array with their score and nickname, sort them in descending order by score and declare a winner, runner up and second runner up (if the game has three players or more). We then publish this info on the `gameRoom` channel so all the clients can switch to the leaderboard screen and display this info.

At the end, we call the `resetServerState()` method which would reset all the counters on the server making it ready to host a new round.

* `resetServerState()`:

{% gist https://gist.github.com/Srushtika/ceb32fab4a5d86a29f3b9d070d9cb063 %}

We reset all the counters and flags to their initial state. We also detach from all the player channels since we no longer need them.

* `startShipAndBullets()`:

{% gist https://gist.github.com/Srushtika/e229d98dbca3851ebc9c9099b592262f %}

This method is called when the required number of players have joined the game, meaning we are ready to start the game.

We start by setting the `gameOn` flag to true. As mentioned before, we'll use the p2 Physics engine on the server-side to manage the movement of the ship. p2 needs a `World` instance to be created. We can set the frequency at which this world moves forward, moving its constituent objects along with it at that speed.

We then create a new `Body` instance for the ship, assign it the inital x/y positions and horizontal/vertical velocities. We add this ship body to the previously created world and call a method to start moving this world. This is when we'd like to start moving the players downwards, so we call that method here.

* `startMovingPhysicsWorld()`:

{% gist https://gist.github.com/Srushtika/1665537d9c9f3eba6d10f26130550b4f %}

We start an interval and move the world with the speed of our choice. We basically update the `shipBody` variable's x/y positions and velocity according to what it is in the physics world at that time. Think of it as the engine moving the ship body with a certain speed towards the right. So if you'd like to know where the ship will be after, say, 2 seconds, the p2 world will tell you exactly that. We can use this info to update the variables that are sent as part of the next game tick update.

* `calcRandomVelocity()`:

{% gist https://gist.github.com/Srushtika/f2cc1f7027077e2abf9b8e1557ea9289 %}

* `randomAvatarSelector()`:

{% gist https://gist.github.com/Srushtika/fb9ae875cc5d323403858e438c7fcb02 %}

The `calcRandomVelocity()` calculates a random velocity which could be either negative (left) or positive (right). The `randomAvatarSelector()` simply returns a random number between 1 and 3, so each player can get assigned a random avatar type and colour out of the three we have available.

---

That's it on the server side. In the [next chapter](https://dev.to/ablydev/building-a-realtime-multiplayer-browser-game-in-less-than-a-day-part-4-4-3d2j), we'll get back to the `script.js` file and finish up the game logic.

All articles in this series:

- Part 1: [Introduction to gaming concepts and the Phaser library](https://dev.to/srushtika/building-a-realtime-multiplayer-browser-game-in-less-than-a-day-part-1-4-14pm)
- Part 2: [Evaluating networking protocols for realtime apps](https://dev.to/ablydev/building-a-realtime-multiplayer-browser-game-in-less-than-a-day-part-2-4-1p1l)
- Part 3: [Implementing the server-side code to keep all players in sync](https://dev.to/ablydev/building-a-realtime-multiplayer-browser-game-in-less-than-a-day-part-3-4-4bbm)
- Part 4: [Finishing up the client-side code to render the game](https://dev.to/ablydev/building-a-realtime-multiplayer-browser-game-in-less-than-a-day-part-4-4-3d2j)

A [separate release](https://github.com/Srushtika/realtime-multiplayer-space-invaders/releases/tag/v1.0.1) relevant to this tutorial is available on GitHub if you'd like to check it out.

You can also [follow the Github project for latest developments](https://github.com/Srushtika/realtime-multiplayer-space-invaders) on this project.

---

As usual, if you have any questions, please feel free to reach out to me on Twitter [@Srushtika](https://twitter.com/Srushtika/). My DMs are open :)