Git Product home page Git Product logo

have_you_heard_back's Introduction

Have you heard - back-end

A game about misunderstanding.

Dependencies

This is a node.js module using socket.io, redis, cluster, and express.

With git and npm installed, you can run:

$ git clone https://github.com/toshisasaki/have_you_heard_back.git
$ cd have_you_heard_back
$ npm install

Check the package.json content for details.

How to run

To test and run, you need a redis instance running. For development and debug, it is assumed that the redis instance is running on localhost on default port 6379.

You may set the environment variable REDIS_URL pointing to a different address and port.

On Fedora, you can install redis by running:

$ sudo dnf install redis

Then, to start the service:

$ systemctl start redis

To run localy, you can run:

$ npm start

or

$ node src

To make the console output verbose for debugging, you can set the DEBUG environment variable:

$ DEBUG=* node --trace-warnings src

By default, the server is listening on http://localhost:3000. You can set a different port by setting the environment variable PORT.

With the server running, the socket.io clients can connect and exchange messages.

Implementation details

The server starts a cluster which starts, by default, 3 worker processes. The main process will restart a new worker process to replace a dead worker.

Each worker process starts a socket.io server and connects to the main process using the cluster adapter.

Then each worker creates 3 connections to the redis instance, being a subscriber, a publisher, and a normal IO connection. The publisher/subscriber pair are used to maintain the socket.io connections in sync with other servers connected to the same redis instance. The IO connection is used to get and store information as keys from redis.

redis is a REmote DIctionary Server, and stores key/value pairs. In this project, we use the keys to store JSON strings that represent some data structures.

The socket.io connections are used to exchange messages in real time with the clients. Each message triggers an event which are handled and the result is reflected to the state in redis, if necessary.

The server is stateless and all the state is stored in redis. This allow servers to be added or removed almost transparently. This will help us scale up and down.

More details

The core of the implementation are two parts: the services and the event handlers.

The event handlers (in src/events) react to events triggered by messages from socket.io and request the services to process the request.

The services (in src/server) implement the abstraction of units responsible for providing functionality to change the state stored in redis.

Currently, the following services are implemented:

  • Redis service: get and store data from redis
  • Users service: manipulates users (create, update, destroy, etc)
  • Rooms service: manipulate rooms
  • Server service: provide access to the socket.io namespace (io) and the express (ex) singleton instances.

The services are implemented as classes, which provide functionality through class methods.

When the server is started, the services are initialized, creating their singleton instances. Then when the events are triggered by messages from the socket.io connection, the event handlers can call the class methods to execute asynchronously the request.

To prevent conflicts and inconsistent states when modifying data in redis, we use transactions with optimistic locks provided by the watch function from redis.

Using watch we can set a list of keys that we are modifying in a transaction. If any of the keys were modified in parallel, the transaction is not applied and should be resubmitted.

Currently we retry up to 5 times when the transaction was interrupted due to conflict. The transactions are not resubmitted case they were interrupted by other errors.

have_you_heard_back's People

Contributors

ansasaki avatar toshisasaki avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.