We are now, simply, Redis

Learn More

How to build a Real-Time Geo-distributed Multiplayer Top-down arcade shooting game using Redis

As the gaming industry continues to grow in size, the need to create a unique and dynamic user experience has become even more mandatory. Because of its fandom, businesses have to maximize the multiplayer gaming experience to drive customer acquisition and retention. However, companies are faced with a number of obstacles when trying to scale multiplayer games, all of which can be solved through Redis. 

Personalized interactions and high-speed reactions are at the heart of creating a unique user experience. Redis provides game publishers with a powerful database that can support low-latency gaming use cases. Recently a Launchpad App built a unique application that could have only been deployed by Redis due to its unmatched speed in transmitting data. 

This is crucial because participants play from all around the world. Interactions between players must be executed in real-time to support gameplay, requiring the latency to be less than one millisecond. 

Let’s take a deep dive into how this was done. But before we do so, make sure to have a browse through the exciting range of different applications that we have on the Launchpad

  1. What will you build?
  2. What will you need?
  3. Architecture
  4. Getting started
  5. How to use the RedisGears function list
  6. Conclusion: overcoming geographical barriers with phenomenal active 

1. What will you build?

You’ll build a real-time Geo-distributed multiplayer top-down arcade shooting game using Redis. The backbone of the application is Redis, for it acts as an efficient real-time database that enables you to store and distribute data. 

As we progress through each stage chronologically, we’ll unpack important terminology as well as each Redis function. 

2. What will you need?

Let’s identify the different components you’ll need to create this game. The application consists of 3 main components:

  • Redis: used as a database, cache, and message broker.
  • RedisGears: defines game function and enables interactions with the game state and user events
  • RediSearch: enables robust querying experience
  • JavaScript client: captures user inputs and sends inputs to the backend. Also uses phaser 3 engine for rendering and physics simulations 3 game engine 
  • NodeJS backend and WebSocket: Communication interface between users and Redis. Primarily used for building the game logic and enabling Redis API

3. Architecture

The main idea of this multiplayer game is to keep it real-time and distribute it geographically. That means that all the instances in your cluster should be updated so that you don’t run out of synchronization. Now let’s take a look at the architecture. 

  • NodeJS forms a communication interface between the players and the Redis database.
  • JavaScript and Phaser 3 library is used for rendering the game state and passing user input to Redis via NodeJS
  • RedisGears is used to enable interaction with the Game State and user events

4. How it works

Now let’s take a look at the flow of the architecture. 

  • Everything begins by using Javascript, which then flows through to Node JS (or WebSocket), in-between the user and backend which is Redis. 
  • Players manoeuvre their character through the terrain by using the arrow keys 
  • Missiles are launches at enemies by clicking the mouse button 
  • All of these commands are being sent through WebSocket to Redis
  • RedisGears parses these commands and updates the state by determining whether the command is valid or not
  • This will distribute the commands back to other users and players
  • If the command is wrong, RedisGears will remove certain players from the game 
  • User navigates through the terrain using the arrow keys and fires missiles at opponents by clicking on the mouse
  • When the user carries out an action, these commands are sent to RedisGears via the WebSocket. 
  • Valid commands will then be redistributed to other users via Redis Streams. 
  • NodeJS forms a communication channel between the user and Redis 
  • A user is able to find a game and join it.
  • It allows users to invite other users to join the game
  • In case, the game is not available, it allows users to create a new game too.
  • RedisGears in turn enables the interaction with the game state  and user events
  • RedisGears parses the data and determines where they should be distributed based on the user commands. 
  • RediSearch allows users to search for a game 

5. Getting started

Prerequisites 

  • Docker
  • Docker-compose

Clone the repository

Under the root of the repository, you will find a Docker compose YAML file:

Under this YAML file, there are two major services that are defined – redis and backend.

Below is how the Dockerfile for Redis looks like:

Below is the Dockerfile for NodeJS backend:

Bringing up the services

Ruin the following commands from the online_game directory

You access the online WebServer via http://127.0.0.1:8080

6. How to use the RedisGears function list

There are three RedisGears functions that each have their own set of sub-functions:

find_game function

Once a user starts the game, the first thing the user will do is search for a game using RediSearch. If the game is present then that person will try and join the game. If not then RedisGears is triggered to create a new game. See the function below

Once the user has created a new game, then other players will join and everyone will be able to play. 

create_new_game function

If no game is present, then RedisGears is triggered to create a new one. 

Once the user has created a new game, then other players will join and everyone will be able to play. 

create_new_user function

This function adds a new player to the game and it has the same approach as Create_new_game function. Again, RedisGears is triggered which then creates a new user

join_game function

When a user joins the game, RedisGears is triggered to enable the Join_game function. This also increments the player count of the game_instance (HINCRBY). 

leave_game function 

When a user is eliminated from the game or chooses to leave, RedisGears is triggered to facilitate the process. This also automatically reduces the player count and automatically creates a notification to confirm that this action has been completed. 

player_actions functions

During gameplay, players will fire missiles to eliminate other competitors. When a player fires a missile, the below sub-function is then triggered:

If a missile hits another player, then that user will be eliminated from the game. The below code determines whether a player has been hit by a missile. 

Player_actions Stream Reader explained

  1. The client connects to node.js WebSocket server. 
  2. The user is then being subscribed SUBSCRIBED to the game_id Redis PubSub channel.3
  3. From there on in, all channel messages the user is subscribed to (on the backend) are also forwarded to the WebSocket (to the frontend).
  4. MESSAGE_EVENT_HANDLERS object stores event -> function mapping, and on an incoming message one of the message event functions is being called (see function list below).

List of Client message event handler: 

MESSAGE_EVENT_HANDLERSExplanation
p (pose) args: [user_id, x, y, orientation]; A client receives a user_id position update
c(click) args: [user_id, x (where it was clicked at), y (where it was clicked at), angle (from the player position to click position)]; A client receives user_id click event
r (respawn) args: [user_id, x, y]; A client receives user_id has respawned
l (leave) args: [user_id]; A client receives user_id has left the game
j (join) args: [user_id, x, y]A client receives user_id has joined the game, and user_id has spawned in the (x, y) position
uid (user id) args: [is_valid];A client receives the response whether it is possible to find ‘log the user in
gid (game id) args: [is_valid];A client receives if the user is part of the game (is user authorized)
hit args: [user_id]; A client receives a message that user_id has been hit / client can remove user_id from rendering it

RediSearch

RediSearch indexes are registered on container startup in the redis/start_redis.sh

Created Redis Search indexes:

Query to find a game:

Conclusion: Overcoming geographical barriers with phenomenal active-active

As with any Redis database, one of the most adored assets is its ability to transmit data between components with unrivalled efficiency. Yet in this application, without the exceptional latency speed that Active-Active provides, the game would simply not be able to function. 

Gameplay is purely interactive, where users from all around the world react and fire missiles at other players. From start to finish, RedisGears deploys a sequence of functions based on the chronological order of the application set-up. 

This is done with ease due to the efficiency of RedisGears which enables an active-active geo-distributed top-down arcade shooter application to be deployed. 

If you want to find out more about this exciting application you can see the full app on the Redis Launchpad. Also make sure to check out all of the other exciting applications we have available for you.

Who created this application?  

Jānis Vilks

Jānis is a Big Data engineer who works at Shipping Technology. 

If you want to discover more about his work and the projects he’s been involved in, then make sure to visit his GitHub profile here