# shared-redux

> Use redux in multiple node/electron processes and keep them in sync

Latest version **1.0.9** (published 2019-04-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install shared-redux
pnpm add shared-redux
yarn add shared-redux
bun add shared-redux
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2019-04-08 |
| First published | 2019-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 142.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Joël Charles |
| Maintainers | magne4000 |
| Keywords | electron, node, multiprocess, shared, sync, redux, desktop |

## Links

- npm: https://www.npmjs.com/package/shared-redux
- Repository: https://github.com/getstation/shared-redux
- Issues: https://github.com/getstation/shared-redux/issues
- npm.io page: https://npm.io/package/shared-redux

## Dependencies (7)

- [debug](https://npm.io/package/debug.md) ^4.1.0
- [redux](https://npm.io/package/redux.md) ^4.0.1
- [immutable](https://npm.io/package/immutable.md) ^3.8.1
- [transit-js](https://npm.io/package/transit-js.md) ^0.8.861
- [xvfb-maybe](https://npm.io/package/xvfb-maybe.md) ^0.2.1
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.3.1
- [transit-immutable-js](https://npm.io/package/transit-immutable-js.md) ^0.7.0

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.0.9 (latest) — 2019-04-08
- 1.0.8 — 2019-03-20
- 1.0.7 — 2019-02-14
- 1.0.6 — 2019-02-13
- 1.0.5 — 2019-02-12
- 1.0.4 — 2019-02-12
- 1.0.3 — 2019-02-12
- 1.0.2 — 2019-02-12
- 1.0.1 — 2019-02-08
- 1.0.0 — 2019-02-06

## README

# shared-redux
[![CircleCI](https://circleci.com/gh/getstation/shared-redux/tree/master.svg?style=svg)](https://circleci.com/gh/getstation/shared-redux/tree/master)

## Motivations
1) Using redux with electron poses a couple of problems. Processes ([main](https://github.com/electron/electron/blob/master/docs/tutorial/quick-start.md#main-process) and [renderer](https://github.com/electron/electron/blob/master/docs/tutorial/quick-start.md#renderer-process)) are isolated.
2) At [Station](https://github.com/getstation), we have the core the app that runs inside a Worker process (invisible renderer). This implies the following:
    - The worker process acts as the redux server
    - We need a way to have the main process and any other renderer to directly talk to the worker
    - Those 2 points make it tricky to use electron own IPC methods because they force the main process to act as the server

## Differences with electron-redux
- This fork doesn't enforce [FSA](https://github.com/acdlite/flux-standard-action#example)
- Supports only ImmutableJS (for now)
- Change dispatch execution order: the process from where the action is dispatched reduces action immediately instead of waiting for the the main to dispatch action in other processes.
- **Can be used by electron or node in the same way**. This also means that, if used in Electron, any renderer process can act as the "server" instead of the main process.

![shared-redux basic](https://user-images.githubusercontent.com/1098371/52342828-ba9cdc00-2a16-11e9-8a82-9dcee4647711.png)

### The solution
`shared-redux` offers a plug and play solution:
  - Choose a process: it acts as the single source of truth for your redux store
  - Choose how your processes communicate: We leverage [stream-json-rpc](https://github.com/getstation/stream-json-rpc) to have a transport agnostic lib. You can plug any stream-compatible layer or write your own.
    - `stream-json-rpc` already implements a Stream layer for electron own IPC and [node-ipc](https://github.com/RIAEvangelist/node-ipc)

## Install

```sh
# npm
npm install --save shared-redux
# yarn
yarn add shared-redux
```

`shared-redux` comes as redux middleware:

```javascript
// in the server store
import { server } from 'shared-redux';
import { firstConnectionHandler } from 'stream-electron-ipc';
// Or if you want to use node-ipc
// import { firstConnectionHandler } from 'stream-node-ipc';

// firstConnectionHandler have the following signature:
// (callback: (socket: Duplex) => void) => void;
// This method should use own Duplex implementation on top of the protocol you have chosen,
// and call the given callback with the Duplex as a parameter once a new client is connected.
// See https://github.com/getstation/stream-json-rpc/blob/master/packages/stream-electron-ipc/src/index.ts for details.
const { forwardToClients, replayActionServer } = server(firstConnectionHandler);

// reducers are shared amongst processes, so keep them pure!
const todoApp = combineReducers(reducers);

const store = createStore(
  todoApp,
  initialState, // optional
  applyMiddleware(
    ...otherMiddleware,
    forwardToClients, // IMPORTANT! This goes last
  )
);

replayActionServer(store);
```

```javascript
// in the client store
import { client } from 'shared-redux';
import { ElectronIpcRendererDuplex } from 'stream-electron-ipc';
// Or any other protocol wrapped in a Duplex

const duplex = new ElectronIpcRendererDuplex();

const { forwardToServer, getInitialStateClient, replayActionClient } = client(duplex);

// reducers are shared amongst processes, so keep them pure!
const todoApp = combineReducers(reducers);
const initialState = await getInitialStateClient();

const store = createStore(
  todoApp,
  initialState,
  applyMiddleware(
    forwardToServer, // IMPORTANT! This goes first
    ...otherMiddleware,
  )
);

replayActionClient(store);
```

And that's it! You are now ready to fire actions without having to worry about synchronising your state between processes.

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