• Stars
    star
    712
  • Rank 61,062 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The Socket.IO Redis emitter, allowing to communicate with a group of Socket.IO servers from another Node.js process.

The Socket.IO Redis emitter

Build Status NPM version

The @socket.io/redis-emitter package allows you to easily communicate with a group of Socket.IO servers from another Node.js process (server-side).

Emitter diagram

The emitter is also available in other programming languages:

It must be used in conjunction with @socket.io/redis-adapter.

The current version is compatible with both:

  • socket.io-redis@5 (socket.io@2)
  • socket.io-redis@6 (socket.io@3 & socket.io@4)

Table of content

How to use

Installation:

npm i @socket.io/redis-emitter redis

CommonJS

const { Emitter } = require("@socket.io/redis-emitter");
const { createClient } = require("redis"); // not included, needs to be explicitly installed

const redisClient = createClient();

redisClient.connect().then(() => {
  const io = new Emitter(redisClient);

  setInterval(() => {
    io.emit("time", new Date);
  }, 5000);
})

With redis@3, calling connect() is not needed:

const { Emitter } = require("@socket.io/redis-emitter");
const { createClient } = require("redis"); // not included, needs to be explicitly installed

const redisClient = createClient();

const io = new Emitter(redisClient);

setInterval(() => {
  io.emit("time", new Date);
}, 5000);

TypeScript

import { Emitter } from "@socket.io/redis-emitter";
import { createClient } from "redis";

const redisClient = createClient();

redisClient.connect().then(() => {
  const io = new Emitter(redisClient);

  setInterval(() => {
    io.emit("time", new Date);
  }, 5000);
});

With typed events:

import { Emitter } from ".";
import { createClient } from "redis";

interface Events {
  basicEmit: (a: number, b: string, c: number[]) => void;
}

const redisClient = createClient();

redisClient.connect().then(() => {
  const io = new Emitter<Events>(redisClient);

  io.emit("basicEmit", 1, "2", [3]);
});

Emit cheatsheet

const { Emitter } = require("@socket.io/redis-emitter");
const { createClient } = require("redis"); // not included, needs to be explicitly installed

const redisClient = createClient();
const io = new Emitter(redisClient);

// sending to all clients
io.emit(/* ... */);

// sending to all clients in 'room1' room
io.to("room1").emit(/* ... */);

// sending to all clients in 'room1' except those in 'room2'
io.to("room1").except("room2").emit(/* ... */);

// sending to individual socketid (private message)
io.to(socketId).emit(/* ... */);

const nsp = io.of("/admin");

// sending to all clients in 'admin' namespace
nsp.emit(/* ... */);

// sending to all clients in 'admin' namespace and in 'notifications' room
nsp.to("notifications").emit(/* ... */);

Note: acknowledgements are not supported

API

Emitter(client[, opts])

client is a node_redis compatible client that has been initialized with the return_buffers option set to true.

The following options are allowed:

  • key: the name of the key to pub/sub events on as prefix (socket.io)
  • parser: parser to use for encoding messages to Redis (`notepack.io)

Emitter#to(room:String):BroadcastOperator

Emitter#in(room:String):BroadcastOperator

Specifies a specific room that you want to emit to.

Emitter#except(room:String):BroadcastOperator

Specifies a specific room that you want to exclude from broadcasting.

Emitter#of(namespace:String):Emitter

Specifies a specific namespace that you want to emit to.

Emitter#socketsJoin(rooms:String|String[])

Makes the matching socket instances join the specified rooms:

// make all Socket instances join the "room1" room
io.socketsJoin("room1");

// make all Socket instances of the "admin" namespace in the "room1" room join the "room2" room
io.of("/admin").in("room1").socketsJoin("room2");

Emitter#socketsLeave(rooms:String|String[])

Makes the matching socket instances leave the specified rooms:

// make all Socket instances leave the "room1" room
io.socketsLeave("room1");

// make all Socket instances of the "admin" namespace in the "room1" room leave the "room2" room
io.of("/admin").in("room1").socketsLeave("room2");

Emitter#disconnectSockets(close:boolean)

Makes the matching socket instances disconnect:

// make all Socket instances disconnect
io.disconnectSockets();

// make all Socket instances of the "admin" namespace in the "room1" room disconnect
io.of("/admin").in("room1").disconnectSockets();

// this also works with a single socket ID
io.of("/admin").in(theSocketId).disconnectSockets();

Migrating from socket.io-emitter

The package was renamed from socket.io-emitter to @socket.io/redis-emitter in v4, in order to better reflect the relationship with Redis.

To migrate to the new package, you'll need to make sure to provide your own Redis clients, as the package will no longer create Redis clients on behalf of the user.

Before:

const io = require("socket.io-emitter")({ host: "127.0.0.1", port: 6379 });

After:

const { Emitter } = require("@socket.io/redis-emitter");
const { createClient } = require("redis");

const redisClient = createClient();
const io = new Emitter(redisClient);

License

MIT

More Repositories

1

socket.io

Realtime application framework (Node.JS server)
TypeScript
59,936
star
2

socket.io-client

Realtime application framework (client)
TypeScript
10,492
star
3

socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
Java
5,244
star
4

socket.io-client-swift

Swift
5,128
star
5

engine.io

The engine used in the Socket.IO JavaScript server, which manages the low-level transports such as HTTP long-polling and WebSocket.
JavaScript
4,569
star
6

socket.io-redis-adapter

Adapter to enable broadcasting of events to multiple separate socket.io server nodes.
TypeScript
2,712
star
7

socket.io-client-cpp

C++11 implementation of Socket.IO client
C++
2,181
star
8

socket.io-p2p

JavaScript
1,022
star
9

engine.io-client

The engine used in the Socket.IO JavaScript client, which manages the low-level transports such as HTTP long-polling, WebSocket and WebTransport.
JavaScript
738
star
10

socket.io-protocol

Socket.IO Protocol specification
JavaScript
494
star
11

engine.io-client-java

Engine.IO Client Library for Java
Java
356
star
12

socket.io-admin-ui

Admin UI for Socket.IO
Vue
322
star
13

socket.io-website

Socket.IO website and blog
JavaScript
311
star
14

engine.io-protocol

Engine.IO protocol
JavaScript
229
star
15

socket.io-adapter

The Socket.IO in-memory adapter
TypeScript
195
star
16

engine.io-server-java

Engine.IO Server Library for Java
Java
161
star
17

socket.io-parser

JavaScript
133
star
18

socket.io-deno

Socket.IO server for Deno
TypeScript
88
star
19

engine.io-parser

Parser for the engine.io protocol, used by client and server
TypeScript
75
star
20

socket.io-msgpack-parser

Socket.IO parser based on msgpack
JavaScript
62
star
21

socket.io-sticky

A simple and performant way to use Socket.IO within a cluster.
JavaScript
37
star
22

socket.io-mongo-adapter

The Socket.IO MongoDB adapter, allowing to broadcast events between several Socket.IO servers
TypeScript
21
star
23

socket.io-chat-platform

A basic chat platform based on Socket.IO
JavaScript
19
star
24

socket.io-postgres-adapter

The Socket.IO Postgres adapter, allowing to broadcast events between several Socket.IO servers
TypeScript
18
star
25

socket.io-redis-streams-adapter

The Socket.IO adapter based on Redis Streams, allowing to broadcast events between several Socket.IO servers.
TypeScript
17
star
26

socket.io-cluster-adapter

The Socket.IO official cluster adapter, allowing to broadcast events between several Socket.IO servers.
TypeScript
14
star
27

socket.io-json-parser

socket.io parser based on JSON.stringify / JSON.parse
JavaScript
12
star
28

socket.io-minimal-example

Socket.IO minimal example
HTML
8
star
29

socket.io-benchmarks

Benchmarks for Socket.IO
JavaScript
8
star
30

socket.io-compression-demo

JavaScript
7
star
31

get-started-drawing

7
star
32

lz77-compression-demo

JavaScript
7
star
33

socket.io-mongo-emitter

The Socket.IO MongoDB emitter, allowing to communicate with a group of Socket.IO servers from another Node.js process
TypeScript
6
star
34

socket.io-postgres-emitter

The Socket.IO Postgres emitter, allowing to communicate with a group of Socket.IO servers from another Node.js process.
TypeScript
5
star
35

socket.io-echo-server

Socket.IO echo server
JavaScript
4
star
36

socket.io-sample-playbook

This repository contains an Ansible playbook to set up a basic Socket.IO application.
JavaScript
4
star
37

tap-to-android

3
star
38

socket.io-swift-fiddle

Swift
2
star
39

socket.io-browsers

A reusable list of browsers to test when using defunctzombie/zuul
JavaScript
1
star