# shared-state-machine

> A simple library for shared state-machines between client and server. It was designed to simplify developing web-apps. Client-server communication is often done through APIs, but, (i) designing APIs is laborous; (ii) calling the API in the middle of the a

Latest version **0.1.6** (published 2017-06-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install shared-state-machine
pnpm add shared-state-machine
yarn add shared-state-machine
bun add shared-state-machine
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.6 |
| Published | 2017-06-08 |
| First published | 2017-03-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | MaiaVictor |
| Maintainers | maiavictor |

## Links

- npm: https://www.npmjs.com/package/shared-state-machine
- Repository: https://github.com/maiavictor/shared-state-machine
- Homepage: https://github.com/maiavictor/shared-state-machine#readme
- Issues: https://github.com/maiavictor/shared-state-machine/issues
- npm.io page: https://npm.io/package/shared-state-machine

## Dependencies (1)

- [servify](https://npm.io/package/servify.md) ^0.1.11

## Recent versions

- 0.1.6 (latest) — 2017-06-08
- 0.1.5 — 2017-06-08
- 0.1.4 — 2017-03-15
- 0.1.3 — 2017-03-15
- 0.1.2 — 2017-03-15
- 0.1.1 — 2017-03-15
- 0.1.0 — 2017-03-14

## README

## Shared State Machine

A simple library for shared state-machines between client and server. It was designed to simplify developing web-apps. Client-server communication is often done through APIs, but, (i) designing APIs is laborous; (ii) calling the API in the middle of the application is an imperactive, state-mutating operation. Instead, `shared-state-machine` allows you to just define the logic of your app as an initial state and a transaction function. The library then takes care of creating the server, and letting the clients send transactions, and stay synched with the machine's state.

## Example: shared random-walk app

This demo implements a random-walk app. Its state is just a number, and it has only one transaction type, which adds a number to the state.

### 1. Install the lib

    npm install shared-state-machine

### 2. Specify the app

The app specification is just a combination of the initial state and the transaction function.

```javascript
const walkerApp = {
  init: 0,
  next: tx => state => tx + state
}
```

### 3. Start the master state-machine (i.e., the "server")

To start the server, you just need the App specification and a port.

```javascript
const ssm = require("shared-state-machine");

// Inits master machine
ssm.init(walkerApp, 7171).then(() =>
  console.log("Started master state machine."));
```

### 4. Play the state-machine remotely (i.e., the "clients")

To start a client, you just need the App specification and the server URL. It returns a state-machine object, which allows you to send a transaction with `ssm.act(tx)`, and to observe state changes with `ssm.get(state => ...)`.

```javascript
const ssm = require("shared-state-machine");

 // Plays the state machine remotely
ssm.play(app, "http://localhost:7171").then(ssm => {

  // When the state changes, prints it
  ssm.get(state => console.log("state: " + state));

  // Every second, add a random number to the state
  setInterval(() => ssm.act(Math.random() - 0.5), 1000);

});
```

## Implementation

The library is a very thin, 70-LOC file which depends on just another thin, 50-LOC library. It uses HTTP pooling for state replication, so, on the large scale, its performance could be improved using websockets.

## Conclusion

Whenever you have an application where each client has a view of the whole state of the app, and can act to change that state, then using `shared-state-machine` instead of designing an API might be the most robust way to go. It plays really well with React.

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