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-multi-redis

v3.0.2

Published

The Socket.IO Redis adapter, allowing to broadcast events between several Socket.IO servers

Downloads

127

Readme

socket.io-multi-redis

NPM version

The socket.io-multi-redis package allows broadcasting packets between multiple Socket.IO servers.

socket.io-multi-redis Uses Custom Scaling Solution.

By running socket.io with the socket.io-multi-redis adapter you can run multiple socket.io instances in different processes or servers that can all broadcast and emit events to and from each other with any number of redis server connections.

Table of contents

Supported features

| Feature | socket.io version | Support | |---------------------------------|---------------------|------------------------------------------------| | Socket management | 4.0.0 | :white_check_mark: YES (since version 6.1.0) | | Inter-server communication | 4.1.0 | :white_check_mark: YES (since version 7.0.0) | | Broadcast with acknowledgements | 4.5.0 | :white_check_mark: YES (since version 7.2.0) | | Connection state recovery | 4.6.0 | :x: NO |

Installation

npm install socket.io-multi-redis

Compatibility table

| Redis Adapter version | Socket.IO server version | |-----------------------|--------------------------| | 2.x | 2.x | | 3.x | 4.3.x and above |

Usage

With the redis package

import { createClient } from "redis";
import { Server } from "socket.io";
import { createAdapter } from "socket.io-multi-redis";

const pubClient1 = createClient({ url: "redis://localhost:6379" });
const subClient1 = pubClient.duplicate();

const pubClient2 = createClient({ url: "redis://localhost:6380" });
const subClient2 = pubClient.duplicate();

const pubClient3 = createClient({ url: "redis://localhost:6381" });
const subClient3 = pubClient.duplicate();

await Promise.all([
  pubClient1.connect(),subClient1.connect(),
  pubClient2.connect(),subClient2.connect(),
  pubClient3.connect(),subClient3.connect()
]);

const io = new Server({
  adapter: createAdapter(
    [pubClient1, pubClient2, pubClient3],
    [subClient1, subClient2, subClient3],
  )
});

io.listen(3000);

With the redis package and a Redis cluster

import { createCluster } from "redis";
import { Server } from "socket.io";
import { createAdapter } from "socket.io-multi-redis";

const pubClient = createCluster({
  rootNodes: [
    {
      url: "redis://localhost:7000",
    },
    {
      url: "redis://localhost:7001",
    },
    {
      url: "redis://localhost:7002",
    },
  ],
});
const subClient = pubClient.duplicate();

await Promise.all([
  pubClient.connect(),
  subClient.connect()
]);

const io = new Server({
  adapter: createAdapter(pubClient, subClient)
});

io.listen(3000);

With the ioredis package

import { Redis } from "ioredis";
import { Server } from "socket.io";
import { createAdapter } from "socket.io-multi-redis";

const pubClient1 = new Redis();
const subClient1 = pubClient.duplicate();

const pubClient2 = new Redis();
const subClient2 = pubClient.duplicate();

const pubClient3 = new Redis();
const subClient3 = pubClient.duplicate();

const io = new Server({
  adapter: createAdapter(
    [pubClient1, pubClient2, pubClient3],
    [subClient1, subClient2, subClient3],
  )
});

io.listen(3000);

With the ioredis package and a Redis cluster

import { Cluster } from "ioredis";
import { Server } from "socket.io";
import { createAdapter } from "socket.io-multi-redis";

const pubClient = new Cluster([
  {
    host: "localhost",
    port: 7000,
  },
  {
    host: "localhost",
    port: 7001,
  },
  {
    host: "localhost",
    port: 7002,
  },
]);
const subClient = pubClient.duplicate();

const io = new Server({
  adapter: createAdapter(pubClient, subClient)
});

io.listen(3000);

With Redis sharded Pub/Sub

Sharded Pub/Sub was introduced in Redis 7.0 in order to help scaling the usage of Pub/Sub in cluster mode.

Reference: https://redis.io/docs/interact/pubsub/#sharded-pubsub

A dedicated adapter can be created with the createShardedAdapter() method:

import { Server } from "socket.io";
import { createClient } from "redis";
import { createShardedAdapter } from "socket.io-multi-redis";

const pubClient = createClient({ host: "localhost", port: 6379 });
const subClient = pubClient.duplicate();

await Promise.all([
  pubClient.connect(),
  subClient.connect()
]);

const io = new Server({
  adapter: createShardedAdapter(pubClient, subClient)
});

io.listen(3000);

Minimum requirements:

Note: it is not currently possible to use the sharded adapter with the ioredis package and a Redis cluster (reference).

Options

Default adapter

| Name | Description | Default value | |------------------------------------|-------------------------------------------------------------------------------|---------------| | key | The prefix for the Redis Pub/Sub channels. | socket.io | | requestsTimeout | After this timeout the adapter will stop waiting from responses to request. | 5_000 | | publishOnSpecificResponseChannel | Whether to publish a response to the channel specific to the requesting node. | false | | parser | The parser to use for encoding and decoding messages sent to Redis. | - |

Sharded adapter

| Name | Description | Default value | |--------------------|-----------------------------------------------------------------------------------------|---------------| | channelPrefix | The prefix for the Redis Pub/Sub channels. | socket.io | | subscriptionMode | The subscription mode impacts the number of Redis Pub/Sub channels used by the adapter. | dynamic |

License

MIT