# socket-redux-middleware

> Redux middleware and React hook that wire a Socket.IO connection to dispatch, so server-sent events and client actions flow straight into your Redux store.

Latest version **3.0.0** (published 2026-09-07) · 0 weekly downloads

## Install

```sh
npm install socket-redux-middleware
pnpm add socket-redux-middleware
yarn add socket-redux-middleware
bun add socket-redux-middleware
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2026-09-07 |
| First published | 2023-05-24 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 39.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | kartik.pal |
| Keywords | redux, middleware, socket.io |

## Links

- npm: https://www.npmjs.com/package/socket-redux-middleware
- npm.io page: https://npm.io/package/socket-redux-middleware

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2026-09-07
- 2.0.6 — 2026-09-07
- 2.0.5 — 2026-09-07
- 2.0.4 — 2026-09-07
- 2.0.3 — 2026-09-07
- 2.0.2 — 2026-09-07
- 2.0.1 — 2026-09-07
- 2.0.0 — 2023-09-26
- 1.0.9 — 2023-09-26
- 1.0.6 — 2023-09-26
- 1.0.5 — 2023-09-26
- 1.0.4 — 2023-09-26
- 1.0.2 — 2023-09-26
- 1.0.1 — 2023-06-25
- 1.0.0 — 2023-05-24

## README

# socket-redux-middleware

Redux middleware and a React hook that connect a [Socket.IO](https://socket.io/)
client to a Redux store: actions emitted by the server are dispatched
automatically, and actions matching your own criteria are emitted to the
server before continuing through the middleware chain.

## Installation

```bash
npm install socket-redux-middleware
```

`react`, `react-redux`, `redux`, and a Socket.IO client instance are peer
dependencies — install/configure them in your app if you haven't already.

## Usage

### 1. Wire up the middleware

```js
import { createStore, applyMiddleware } from "redux";
import io from "socket.io-client";
import { socketIoMiddleware } from "socket-redux-middleware";
import rootReducer from "./reducers";

const socket = io("https://example.com");

const store = createStore(
  rootReducer,
  applyMiddleware(socketIoMiddleware(socket, ["server/"]))
);
```

- Actions emitted **by the server** under the event name `"action"` are
  dispatched into the store automatically.
- Actions dispatched **on the client** whose `type` matches the `criteria`
  (a string prefix, an array of prefixes, or a `(type, action) => boolean`
  function) are emitted to the server via `socket.emit` instead of (or in
  addition to) being passed down the middleware chain.

### 2. Dispatch from a component

```jsx
import { useSocketDispatch } from "socket-redux-middleware";

function Chat() {
  const socketDispatch = useSocketDispatch();

  const sendMessage = (text) => {
    socketDispatch("server/message", { text });
  };

  return <button onClick={() => sendMessage("hi")}>Send</button>;
}
```

`useSocketDispatch` returns a stable callback that dispatches
`{ type, payload, keyName, keyType }` — pair it with the `criteria` you gave
`socketIoMiddleware` so those actions get routed to the server.

## API

### `socketIoMiddleware(socket, criteria, options)`

| Param               | Type                                              | Default                      | Description                                                                                       |
| ------------------- | ------------------------------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------- |
| `socket`            | `Socket`                                          | required                     | A connected Socket.IO client instance.                                                            |
| `criteria`          | `string \| string[] \| (type, action) => boolean` | `[]`                         | Selects which dispatched actions get sent to the server. A string/array matches by `type` prefix. |
| `options.eventName` | `string`                                          | `"action"`                   | Socket.IO event name used both for actions received from the server and actions emitted to it.    |
| `options.execute`   | `(action, emit, next, dispatch) => any`           | emits + calls `next(action)` | Override how a matching action is handled instead of the default emit-then-continue behavior.     |

### `useSocketDispatch()`

Returns `socketDispatch(type, payload, keyName, keyType)`, a memoized
callback that dispatches `{ type, payload, keyName, keyType }` through
`react-redux`'s `useDispatch`.

## License

MIT

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