npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

socket.io-distributor

v0.2.8

Published

Transparent load leveling for socket.io

Downloads

2,486

Readme

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.