# ioredis-streams-adapter

> The Socket.IO adapter based on Redis Streams, allowing to broadcast events between several Socket.IO servers

Latest version **0.1.0** (published 2023-05-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install ioredis-streams-adapter
pnpm add ioredis-streams-adapter
yarn add ioredis-streams-adapter
bun add ioredis-streams-adapter
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2023-05-31 |
| First published | 2023-05-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14.0.0 |
| Dependencies | 2 |
| Unpacked size | 33.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 42 |
| Maintainers | safiresh |
| Keywords | socket.io, redis, adapter |

## Links

- npm: https://www.npmjs.com/package/ioredis-streams-adapter
- Repository: https://github.com/socketio/socket.io-redis-streams-adapter
- Homepage: https://github.com/socketio/socket.io-redis-streams-adapter#readme
- Issues: https://github.com/socketio/socket.io-redis-streams-adapter/issues
- npm.io page: https://npm.io/package/ioredis-streams-adapter

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ~4.3.1
- [@msgpack/msgpack](https://npm.io/package/@msgpack/msgpack.md) ~2.8.0

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2023-05-31

## README

# Socket.IO Redis Streams adapter

The `@socket.io/redis-streams-adapter` package allows broadcasting packets between multiple Socket.IO servers.

Supported features:

- [broadcasting](https://socket.io/docs/v4/broadcasting-events/)
- [utility methods](https://socket.io/docs/v4/server-instance/#Utility-methods)
  - [`socketsJoin`](https://socket.io/docs/v4/server-instance/#socketsJoin)
  - [`socketsLeave`](https://socket.io/docs/v4/server-instance/#socketsLeave)
  - [`disconnectSockets`](https://socket.io/docs/v4/server-instance/#disconnectSockets)
  - [`fetchSockets`](https://socket.io/docs/v4/server-instance/#fetchSockets)
  - [`serverSideEmit`](https://socket.io/docs/v4/server-instance/#serverSideEmit)
- [connection state recovery](https://socket.io/docs/v4/connection-state-recovery)

Related packages:

- Redis adapter: https://github.com/socketio/socket.io-redis-adapter/
- Redis emitter: https://github.com/socketio/socket.io-redis-emitter/
- MongoDB adapter: https://github.com/socketio/socket.io-mongo-adapter/
- MongoDB emitter: https://github.com/socketio/socket.io-mongo-emitter/
- Postgres adapter: https://github.com/socketio/socket.io-postgres-adapter/
- Postgres emitter: https://github.com/socketio/socket.io-postgres-emitter/

**Table of contents**

- [Installation](#installation)
- [Usage](#usage)
- [Options](#options)
- [How it works](#how-it-works)
- [License](#license)

## Installation

```
npm install @socket.io/redis-streams-adapter redis
```

## Usage

```js
import { createClient } from "redis";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-streams-adapter";

const redisClient = createClient({ url: "redis://localhost:6379" });

await redisClient.connect();

const io = new Server({
  adapter: createAdapter(redisClient)
});

io.listen(3000);
```

## Options

| Name                | Description                                                        | Default value |
|---------------------|--------------------------------------------------------------------|---------------|
| `streamName`        | The name of the Redis stream.                                      | `socket.io`   |
| `maxLen`            | The maximum size of the stream. Almost exact trimming (~) is used. | `10_000`      |
| `readCount`         | The number of elements to fetch per XREAD call.                    | `100`         |
| `heartbeatInterval` | The number of ms between two heartbeats.                           | `5_000`       |
| `heartbeatTimeout`  | The number of ms without heartbeat before we consider a node down. | `10_000`      |

## How it works

The adapter will use a [Redis stream](https://redis.io/docs/data-types/streams/) to forward events between the Socket.IO servers.

Notes:

- a single stream is used for all namespaces
- the `maxLen` option allows to limit the size of the stream
- unlike the adapter based on Redis PUB/SUB mechanism, this adapter will properly handle any temporary disconnection to the Redis server and resume the stream
- if [connection state recovery](https://socket.io/docs/v4/connection-state-recovery) is enabled, the sessions will be stored in Redis as a classic key/value pair

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/ioredis-streams-adapter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
