# redux-session-server

> Redux based server side session management

Latest version **1.0.3** (published 2017-11-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-session-server
pnpm add redux-session-server
yarn add redux-session-server
bun add redux-session-server
```

## 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.3 |
| Published | 2017-11-21 |
| First published | 2017-11-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | syaau |

## Links

- npm: https://www.npmjs.com/package/redux-session-server
- Repository: https://github.com/Bhoos/redux-session-server
- Homepage: https://github.com/Bhoos/redux-session-server#readme
- Issues: https://github.com/Bhoos/redux-session-server/issues
- npm.io page: https://npm.io/package/redux-session-server

## Dependencies (1)

- [redux-batched-actions](https://npm.io/package/redux-batched-actions.md) ^0.1.6

## Recent versions

- 1.0.3 (latest) — 2017-11-21
- 1.0.2 — 2017-11-17
- 1.0.0 — 2017-11-17

## README

# redux-session-server
Using redux on server with proper session management

# Installation
> `$ npm install redux-session-server`

# Usage
## Create a session manager redux middleware
```javascript
import { createSessionManager } from 'redux-session-server';

const onNewSession = (session, getState, dispatch) => { 
  // Callback mechanism to handle new session, may be see if there
  // are more sessions than we can support. Just throw and error if
  // you do not want to accept this session
}

const onDestroySession = (session, getState, dispatch) => {
  // A notification mechanism when a session is destroyed. Could be
  // used as a cleanup mechanism
}

const sessionManager = createSessionManager(onNewSession, onDestroySession);
const store = createStore(reducer, applyMiddleware(thunk, sessionManager));

// Create sessions via sessionManager for every new user connection
// The userId uniquely identifies the session
// Timestamp could be used for changing timestamps through preProcessing action hooks
// serialId could be used to dispatch only actions that are available after the given id
// client needs to provide a 'close' and 'dispatch' mechanisms.
const clientSession = sessionManager.createSession(userId, Date.now(), 0, client);

// the clientSession can then be used to perform any user Action
clientSession.join(name, picture);
clientSession.makeMove([1, 2]);
```

## Register user actions
```javascript
import { registerUserAction } from 'redux-session-server';

const join = (session, name, picture) => (dispatch, getState) => {
  dispatch({
    type: 'JOIN',
    payload: {
      id: session.id,
      name,
      picture,
    },
  });
}

// Register user actions, that are then available to be invoked via clientSession.
// Note that, while invoking the action from the client session object, the session
// instance should not be passed, it is provided by the manager itself
registerUserAction('join', join);
```

## Add hooks to customize your action structure
You could use hooks to clear actions from previous rounds, or remove selective actions
only. For example, after every LEAVE action, you might want to remove the JOIN action.
This way your list of actions remain trimmed.

```javascript
import { registerActionHook } from 'redux-session-server'

registerActionHook('LEAVE', {
  // As soon as the action is executed on the store, this hook is triggered. You
  // could use the `actions` parameter to trim down on your actions list.
  // You could change the action here (hardly needed), or you could return null
  // to not record the action in serialization
  preProcess: (action, actions) => {
    // Remove only one (iterating all the actions from the back)
    actions.removeOne(a => a.type === 'LEAVE');
    // Use removeOneForward to iterate from the front

    // Remove all actions that are tagged as dynamic
    actions.remove(a => a.tag === 'dyn');

    // If you want this action to be recorded for serialization return it, otherwise
    // return null;
    return action;
  },

  // Sanitize the action on the per session basis, this hook it triggered before the
  // action is dispatched to the client. Use this opportunity to customize the action
  // on per client basis. For example you might want to remove client specific information
  // which are all needed on server, but should not be passed on to the client. Or you
  // might want to change the timestamps of your timer, so all the clients behave exactly
  // regardles of what time they have.
  sanitize: (action, session) => ({
    ...action,
    payload: {
      [session.id]: action.payload[session.id],
      startTimestamp: action.payload.startTimestamp - session.delta,
    },
  });
})
```

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