WebSocket Server

Component Documentation

Documentation for each component is linked below.

Introduction

The server was developed in response to low performance and intermittent connection issues that resulted from implementing WebSocket connections using Django Channels. After further research, our team determined that Django Channels was not able to meet our needs when migrating to high-performance multiplayer games, which required high concurrency.

In our checkoff on 5/9, we demonstrated a working implementation of Vegetable Assassin using Django Channels as the sole backend. However, as we began implementing multiplayer support, we realized that the database-backed WebSocket handler implemented in Django Channels was suitable for low-volume and latency-insensitive traffic such as chat messages, but not the high-volume and latency sensitive Vegetable Assassin traffic.

To solve this issue, we decided to split our system into two different server applications: the statistics server, which is still implemented using Django Channels, and the WebSocket server, which utilizes Node.js for improved performance due to the native support for connection protocols such as WebSockets.

The WebSocket server consists of less than 50 lines of code that perform the following function:

  • On a client connection, stores the client mapped to the session ID in a dictionary-mapping within memory, which is higher performance than the Redis database.
  • On a message to a WebSocket channel, emits that message to all other connected clients on the channel.
  • Garbage collects clients that have disconnected on every emitted message to minimize performance impact while reducing memory usage whenever possible.
  • Forwards messages that contain statistics after a game's conclusion to the statistics server through a temporary WebSocket connection.

Installation

Installing is incredibly simple. Ensure that you have the latest version of Node.js and npm installed.

  1. Clone the repo to an empty folder.
    git clone https://github.com/TypingKoala/Vegetable-Assassin-WS-Server
    
  2. Enter the folder.
    cd Vegetable-Assassin-WS-Server
    
  3. Install dependencies using npm.
    npm install
    
  4. Run the server.
    node index.js