Giter Club home page Giter Club logo

socket.io-distributor's Introduction

socket.io-distributor

Transparent load leveling and multiplexing for socket.io. Transfers events via redis like socket.io-redis but allows two-way communication.

Install via npm: socket.io-distributor.

Source code on GitHub: phiresky/socket.io-distributor

Works great with typed-socket.io.

Motivation

Say you have multiple socket.io backends, for example a chat and two games.

Client1 wants to connect the chat and gameA, and Client2 wants to connect to the chat and gameB.

Without this library, both clients need to open two connections (to each backend):

Before

With this library, each client only connects to one distributor (it doesn't matter which), and that in turn connects to the wanted backends via redis using socket.io namespaces:

After

Now, when a backend emits an event to everyone or to everyone in a room, it only has to send a single event instead of n events (one for each client). When a server sends an event to a single client, it only goes to the correct distributor, so each distributor has the load of only its own clients.

You can also add a reverse proxy like nginx before the distributors, that then chooses a random distributor using multiple upstreams. That way everything will look from the client side like a single socket.io server with multiple namespaces.

Usage

On your backend, instead of doing this:

import socketio from "socket.io";
import http from "http";
const server = http.createServer();
server.listen(8000);
const io = io(server).of("/chat");

Do this:

import { indirectSocketViaRedis } from "socket.io-distributor/backend";

const io = indirectSocketViaRedis<MyServerDefinition, "/chat">({
    namespace: "/chat",
    redis: { uri: "redis://localhost:6379" },
});

The io object will work basically the same as before.

Then you start a few distributor processes that look like this:

import { Worker } from "socket.io-distributor/distributor";

const slaveId = +process.argv[2];
const server = http.createServer();
server.listen(8000 + slaveId);
const worker = new Worker<MyServerDefinition>({
    server,
    slaveId,
    namespaces: ["/chat" /*...*/],
    redis: { uri: "redis://localhost:6379" },
});

Now clients can connect to any of http://localhost:8001, http://localhost:8002, etc, and they will be able to send and receive messages from the backend.

socket.io-distributor's People

Contributors

phiresky avatar msteknoadam avatar

Stargazers

GAURAV avatar  avatar Tino Fuhrmann avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

icodein

socket.io-distributor's Issues

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.