All Projects → socketio → socket.io-redis-emitter

socketio / socket.io-redis-emitter

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

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to socket.io-redis-emitter

m-server
egg.js简单应用
Stars: ✭ 35 (-94.8%)
Mutual labels:  socket-io
python react blog back end
Redis Celery Fabric Gunicorn Personal Blog
Stars: ✭ 12 (-98.22%)
Mutual labels:  socket-io
react-app-simple-chat-app
A Simple Chat Application using MERN stack (MongoDB, Express JS, React JS, Node JS) and Socket.io for real time chatting
Stars: ✭ 41 (-93.91%)
Mutual labels:  socket-io
Introduction p5js
This is a french introduction to p5*js
Stars: ✭ 29 (-95.69%)
Mutual labels:  socket-io
laravel-echo-redis-socketio
It's a very simple chat demo, use laravel-echo-server and Laravel Echo
Stars: ✭ 30 (-95.54%)
Mutual labels:  socket-io
node-server-template
This is Node.js server tidy template / boilerplate with Express (with asyncified handlers, custom error handler) framework and MongoDb. The server use ES6 and above. On different branches you can see different techniques' and technologies' usage, such as Kafka, nodemailer, file download... You also can find postman collections.
Stars: ✭ 116 (-82.76%)
Mutual labels:  socket-io
vega
HTTP Client w/ Team Sharing Features!
Stars: ✭ 20 (-97.03%)
Mutual labels:  socket-io
socketChat
💬 A real-time messaging application built using Socket.IO and Express
Stars: ✭ 31 (-95.39%)
Mutual labels:  socket-io
aioudp
Asyncio UDP server
Stars: ✭ 21 (-96.88%)
Mutual labels:  socket-io
socket.io-rails
Rails asset pipeline wrapper for socket.io
Stars: ✭ 57 (-91.53%)
Mutual labels:  socket-io
tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 2,350 (+249.18%)
Mutual labels:  socket-io
local-party
A website where you can create rooms and chat while watching local video files synchronized with your friends.
Stars: ✭ 196 (-70.88%)
Mutual labels:  socket-io
ChatDemo
A demonstration of private communication on a public channel.
Stars: ✭ 29 (-95.69%)
Mutual labels:  socket-io
Online-Chess
A chess website where people can play against each other online.
Stars: ✭ 28 (-95.84%)
Mutual labels:  socket-io
story-point
👕 Story pointing (or T-shirt sizing) app built with Svelte
Stars: ✭ 20 (-97.03%)
Mutual labels:  socket-io
uptime-kuma
A fancy self-hosted monitoring tool
Stars: ✭ 27,425 (+3975.04%)
Mutual labels:  socket-io
redparty
Host Youtube watch party with friends. Sync videos and chat in real-time
Stars: ✭ 70 (-89.6%)
Mutual labels:  socket-io
chat-app-server
Back-end server for chat application built using express, moongodb & socket.io for Frontend (https://github.com/binbytes/nuxt-chat-app).
Stars: ✭ 30 (-95.54%)
Mutual labels:  socket-io
node-vue-chat
🚀 🌠 Real Time Chat Application created with MEVN Stack
Stars: ✭ 89 (-86.78%)
Mutual labels:  socket-io
socketio
No description or website provided.
Stars: ✭ 23 (-96.58%)
Mutual labels:  socket-io

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)

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].