Posts

Colyseus, an uncomplicated library for multiplayer games

A couple of weeks ago we released our latest game, Mini Pool. It has been a success and Colyseus.io has contributed a lot to achieving this. Our programmers had less stress dealing with server issues and could concentrate on developing game play.

 


We considered to code the server side directly with node and websockets. It isn’t an incredible amount of work but why reinvent the wheel? Colyseus is above all easy to understand and implement. It takes care of the rooms creation, matching and player’s disconnection handling. One of the strengths of this framework is how it manages the room state. Basically we create a custom data structure for the room state and every time those data are changed by the server all clients are informed and act correspondingly. This feature means less messages and less code in the server side.

 

For example the state object of the Room in the server side has a field called “currentTurn”. If the server logic change its value both clients are notified that the turn has changed without having to broadcast manually a message.

 

 

Due to performance reasons the physics engine is implemented in the client side. However the server is authoritative, each turn it validates balls positions and makes sure that both clients are synchronized and positions are consistent.

 

Another interesting feature of Colyseus is that is coded in TypeScript which is precisely the language we use in the client side.

Colyseus is open source, the project is well maintained and has a forum where usually all doubts are solved in matter of hours.

Multiplayer HTML5 games tools

multiplayer html5 games

WebSockets are the most popular technology for developing multiplayer HTML5 games. They allow bidirectional communications between clients and server using a single socket TCP.

We code our multiplayer HTML5 games in Javascript, both the client and the server side. More specifically, we use a game engine called phaser.io for the client and Node.js in the server.

There are several options for working with WebSockets. The most common and simple is Socket.io. We implemented sockets.io on our first projects. However, due to the increasing complexity and demands of our current projects we decided to change to SocketCluster.

Why we love doing HTML5 multiplayer games using SocketCluster

SocketCluster is an open source framework optimized to run with Kubernetes (K8s). It’s the base for our multiplayer development for the following reasons:

  • Because it’s faster and more versatile than socket.io. SocketCluster uses µwebsockets (coded in C++) and offers pub/sub.
  • Highly reliable.
  • Supports custom codecs during transmission to increase data exchange speed.
  • It’s easy to scale. Vertically using multiple cores of the CPU of a single host and horizontally with several servers. It doesn’t require Redis to share the state between processes.

For all of you looking for an introduction to SocketCluster we recommend this complete example of an Agar.io game type:

MultiPlayer game example using Phaser on GitHub.

Finally, here a very useful comparison of the main modules implementing WebSockets:

uWebSockets

Portfolio Items