Presenting a realtime communication framework to build multiplayer games
Tags: gamedev, node, showdev, javascript
Published: 2020-09-04 07:26:25

Hello game developers!

> TL;DR - I built a realtime communication framework so game developers can build multiplayer games without needing to worry about the networking side of it and instead focus on their game logic. It is powered by [Ably's realtime infrastructure](https://ably.com/) which primarily operates on [WebSockets](https://ably.com/topic/websockets) and is built in Node JS and Vanilla JS (use it with any JS framework you like 😉). It also implements Node JS worker threads to simulate multiple 'game rooms' (aka dedicated server spooled up on-demand) so different groups of players can simultaneously play the game. You should check it out: https://github.com/Srushtika/multiplayer-games-scalable-networking-framework

![Demo application](https://dev-to-uploads.s3.amazonaws.com/i/axb5a5lkd3p1an1lp3i3.gif)

A few months back I got into the world of building high-frequency (realtime) multiplayer games to get my boss's attention as he's extremely passionate about game development (Challenge: Try to have a tech conversation with [Ben Gamble](https://twitter.com/BenGamble7/), without game dev coming up!🌚🕹). 

When I started, I had so many misconceptions about game dev. I underestimated certain aspects and had misconceptions about scalable game architectures. But I've learned so much about game dev since then and have built multiplayer versions of a few classics such as [Multiplayer Space Invaders](https://dev.to/ablydev/building-a-realtime-multiplayer-browser-game-in-less-than-a-day-part-1-4-14pm) and [Multiplayer Flappy birds](https://www.youtube.com/watch?v=ReGHyTh1ydU). I've also written and spoken about game dev quite extensively on various platforms. 

![Multiplayer space invaders presented at HalfStack online conference, 2020](https://dev-to-uploads.s3.amazonaws.com/i/fahusd8fhhqq402bqnyu.png)

![Multiplayer Flappy Birds, live gameplay on YouTuber Eddie Jaoude's channel](https://dev-to-uploads.s3.amazonaws.com/i/7e5ovf9nizoob2mczb57.png)


For each such presentation, I made improvements to the games by not only making them more efficient but also having them use the best and latest features of the underlying programming language. For example, I used [Node JS worker threads](https://nodejs.org/api/worker_threads.html) (which was released as a stable version in January 2020) to simulate the idea of having multiple game rooms so different groups of people can play the game simultaneously. This is analogous to spooling up on-demand dedicated servers for each game. [Ably's namespace feature](https://support.ably.com/support/solutions/folders/3000005541) made this strategy possible by ensuring none of the rooms had any access to the data from the other.


## More games = repeating boilerplate? 🤯🥵

While I was continuing to think of more multiplayer game ideas that were cool enough to convince my team to let me work on them during Ably working hours 🌚, I realized some commonalities in all these projects:
All of my multiplayer games so far follow the [Client/Server game strategy](https://www.gabrielgambetta.com/client-server-game-architecture.html). Hence, basic networking architecture is exactly the same and has the following aspects:

* The server maintains the game state and publishes it at high frequency to a [channel](https://ably.com/documentation/realtime/channels) that all the players are subscribed to. This ensures all players are in-sync.

* Each player sends their current state (based on user input, etc) on a unique channel meant for that client. The server is subscribed to this and uses this info to maintain the up to date game state mentioned above.

* The game has multiple game rooms.

* The host player has options to start and end the game.

* In general, there's awareness of every player's score, position, alive/dead status, join/ leave updates for the game, etc.

![Client/Server architecture with each color representing a realtime channel](https://dev-to-uploads.s3.amazonaws.com/i/5kmj63zz5byjefct2lpj.png)


Come to think about it, it seems that this is pretty much what most real-time multiplayer games would need. So, I thought building a networking framework would benefit game developers to quickly add the multiplayer functionality to any game. All the more useful when it's built with Ably, as it means you can easily scale it and have access to so many other realtime communication features (like message ordering, availability, guaranteed message delivery, etc) So, here it is!

## Multiplayer games scalable networking framework

This framework serves as a starter kit that allows you to add multiplayer functionality (that follows the [Client/Server strategy](https://www.gabrielgambetta.com/client-server-game-architecture.html)  to your game. It provides a communication framework so that your players can communicate with a central server, in realtime, for the entire duration of the gameplay.

![Homepage of the demo app that works on this framework](https://dev-to-uploads.s3.amazonaws.com/i/qaxsx6eeovi1ojxtogpa.png)

It also allows you to implement a 'game rooms' feature using Node JS  worker threads, which is analogous to spooling up dedicated servers on-demand, allowing you to spin up multiple instances of the game,  each with a separate group of players. It comes with a skeleton demo app where this game functionality can be simulated. The idea is for game developers to take this framework and add their game logic to make it their own!

![Host waiting area of the demo app that works on this framework](https://dev-to-uploads.s3.amazonaws.com/i/f8f7l4ft56ajy31vbd31.png)

### You can find the full project with a detailed guide on how to use it on GitHub: https://github.com/Srushtika/multiplayer-games-scalable-networking-framework

---

If you have any questions/ suggestions, etc. Feel free to shoot an email to [devrel@ably.io](mailto:devrel@ably.io) or reach out to me directly on [Twitter](https://www.twitter.com/Srushtika), I'll be so glad to talk about this 😃 

If you like it, star it ⭐️ and don't forget to share any multiplayer games you build with this, I'd love to check them out and give a shoutout! 🥇🚀🔥