Skip to content

Repository files navigation

socket.io-valkey-emitter

CI NPM version

The socket.io-valkey-emitter package lets you communicate with a group of Socket.IO servers from another Node.js process — a worker, a cron job, an API service — anything that is not a Socket.IO server itself. It publishes events to Valkey, which the socket.io-valkey-adapter running on your Socket.IO servers picks up and delivers to the connected clients.

Emitter diagram

It is a Valkey-native port of @socket.io/redis-emitter — same API and behavior, backed by Valkey instead of Redis. It works with any ioredis-compatible Valkey client, such as iovalkey.

How it fits together

The diagram above shows the full flow. In short:

  • Emitter (this package) — runs in any Node.js process (a worker, cron job, or API) and pushes events to clients by publishing to Valkey.
  • Adapter (socket.io-valkey-adapter, on your Socket.IO servers) — subscribes to Valkey, makes multiple servers act as one, and delivers the events to the connected clients.

Both must point at the same Valkey.

Installation

npm install socket.io-valkey-emitter iovalkey

Usage

const { Emitter } = require("socket.io-valkey-emitter");
const Valkey = require("iovalkey");

const valkeyClient = new Valkey({ host: "localhost", port: 6379 });
const emitter = new Emitter(valkeyClient);

// broadcast to all connected clients
emitter.emit("time", new Date());

// to a room
emitter.to("room1").emit("hello", "world");

// to a namespace
emitter.of("/admin").emit("event", "payload");

Your Socket.IO servers must use socket.io-valkey-adapter pointed at the same Valkey for the emitted events to reach clients.

API

The emitter mirrors the Socket.IO broadcast API:

  • emitter.emit(event, ...args) — broadcast to all
  • emitter.to(room) / emitter.in(room) — target a room
  • emitter.except(room) — exclude a room
  • emitter.of(namespace) — switch namespace
  • emitter.volatile / emitter.compress(bool) — flags
  • emitter.socketsJoin(rooms) / emitter.socketsLeave(rooms) — remote room management
  • emitter.disconnectSockets(close) — remotely disconnect
  • emitter.serverSideEmit(event, ...args) — send to the servers (no acknowledgements)

License

MIT — a fork of @socket.io/redis-emitter.

About

Socket.IO Valkey emitter — communicate with a group of Socket.IO servers from another Node.js process, backed by Valkey.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages