# @neohelden/node-red-context-redis

> A Node-RED Context store plugin backed by Redis

Latest version **0.1.0** (published 2021-01-21) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @neohelden/node-red-context-redis
pnpm add @neohelden/node-red-context-redis
yarn add @neohelden/node-red-context-redis
bun add @neohelden/node-red-context-redis
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2021-01-21 |
| First published | 2021-01-21 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8 |
| Dependencies | 3 |
| Unpacked size | 61.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | beevelop |
| Keywords | node-red, redis |

## Links

- npm: https://www.npmjs.com/package/@neohelden/node-red-context-redis
- Repository: https://github.com/neohelden/node-red-context-redis
- Homepage: https://github.com/neohelden/node-red-context-redis#readme
- Issues: https://github.com/neohelden/node-red-context-redis/issues
- npm.io page: https://npm.io/package/@neohelden/node-red-context-redis

## Dependencies (3)

- [redis](https://npm.io/package/redis.md) 3.0.2
- [@node-red/util](https://npm.io/package/@node-red/util.md) 1.2.7
- [json-stringify-safe](https://npm.io/package/json-stringify-safe.md) ^5.0.1

## 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) — 2021-01-21

## README

***This is a work in progress.***

# Redis plugin

The Redis plugin holds context data in the Redis.

## Pre-requisite

To run this you need a Redis server running. For details see <a href="http://redis.io/" target="_new">the Redis site</a>.

## Install

1. Run the following command in your Node-RED user directory - typically `~/.node-red`

    npm install git+https://github.com/GruppoFilippetti/node-red-context-redis

2. Add a configuration in settings.js:

```javascript
contextStorage: {
    redis: {
        module: require("node-red-context-redis"),
        config: {
            // see below options
        }
    }
}
```

### Options

This plugin exposes some options defined in [node_redis](https://github.com/NodeRedis/node_redis) as itself options.
It needs following configuration options:

| Options  | Description                                                                                                 |
| -------- | ----------------------------------------------------------------------------------------------------------- |
| host     | The IP address of the Redis server.    `Default: "127.0.0.1"`                                               |
| port     | The port of the Redis server.          `Default: 6379`                                                      |
| db       | The Redis logical database to connect. `Default: 0`                                                         |
| prefix   | If set, the string used to prefix all used keys.                                                            |
| password | If set, the plugin will run Redis AUTH command on connect. *Note: the password will be sent as plaintext.*  |
| tls      | An object containing options to pass to tls.connect to set up a TLS connection to the server.               |
| ttl      | Defaul 10 min

see https://github.com/NodeRedis/node_redis#options-object-properties

## Data Model

```text
Node-RED                      Redis
+-------------------+         +-------------------------------+
| global context    |         | logical database              |
| +---------------+ |         | +---------------------------+ |
| | +-----+-----+ | |         | | +-----------------+-----+ | |
| | | key |value| | | <-----> | | | global:key      |value| | |
| | +-----+-----+ | |         | | +-----------------+-----+ | |
| +---------------+ |         | |                           | |
|                   |         | |                           | |
| flow context      |         | |                           | |
| +---------------+ |         | |                           | |
| | +-----+-----+ | |         | | +-----------------+-----+ | |
| | | key |value| | | <-----> | | | <flow's id>:key |value| | |
| | +-----+-----+ | |         | | +-----------------+-----+ | |
| +---------------+ |         | |                           | |
|                   |         | |                           | |
| node context      |         | |                           | |
| +---------------+ |         | |                           | |
| | +-----+-----+ | |         | | +-----------------+-----+ | |
| | | key |value| | | <-----> | | | <node's id>:key |value| | |
| | +-----+-----+ | |         | | +-----------------+-----+ | |
| +---------------+ |         | +---------------------------+ |
+-------------------+         +-------------------------------+
```

- This plugin uses a Redis logical database for all context scope.
- This plugin prefixes all used keys with context scope in order to identify the scope of the key.
  - The keys of `global context` will be prefixed with `global:` .
    e.g.  Set `"foo"` to hold `"bar"` in the global context -> Set `"global:foo"` to hold `"bar"` in the Redis logical database.
  - The keys of `flow context` will be prefixed with `<id of the flow>:` .
    e.g.  Set `"foo"` to hold `"bar"` in the flow context whose id is `8588e4b8.784b38` -> Set `"8588e4b8.784b38:foo"` to hold `"bar"` in the Redis.
  - The keys of `node context` will be prefixed with `<id of the node>:` .
    e.g.  Set `"foo"` to hold `"bar"` in the node context whose id is `80d8039e.2b82:8588e4b8.784b38` -> Set `"80d8039e.2b82:8588e4b8.784b38:foo"` to hold `"bar"` in the Redis.

## Data Structure

- This plugin converts a value of context to JSON and stores it as string type to the Redis.
- After getting a value from the Redis, the plugin also converts the value to an object or a primitive value.

```text
Node-RED                                 Redis
+------------------------------+         +---------------------------------------------+
| global context               |         | logical database                            |
| +--------------------------+ |         | +-----------------------------------------+ |
| | +--------+-------------+ | |         | | +---------------+---------------------+ | |
| | | str    | "foo"       | | | <-----> | | | global:str    | "\"foo\""           | | |
| | +--------+-------------+ | |         | | +---------------+---------------------+ | |
| | | num    | 1           | | | <-----> | | | global:num    | "1"                 | | |
| | +--------+-------------+ | |         | | +---------------+---------------------+ | |
| | | nstr   | "10"        | | | <-----> | | | global:nstr   | "\"10\""            | | |
| | +--------+-------------+ | |         | | +---------------+---------------------+ | |
| | | bool   | false       | | | <-----> | | | global:bool   | "false"             | | |
| | +--------+-------------+ | |         | | +---------------+---------------------+ | |
| | | arr    | ["a","b"]   | | | <-----> | | | global:arr    | "[\"a\",\"b\"]"     | | |
| | +--------+-------------+ | |         | | +---------------+---------------------+ | |
| | | obj    | {foo,"bar"} | | | <-----> | | | global:obj    | "{\"foo\",\"bar\"}" | | |
| | +--------+-------------+ | |         | | +---------------+---------------------+ | |
| +--------------------------+ |         | +-----------------------------------------+ |
+------------------------------+         +---------------------------------------------+
```

Other Redis client(e.g. redis-cli) can get the value stored by Node-RED like followings.

Node-RED

```javascript
global.set("foo","bar","redis");
global.set("obj",{key:"value"},"redis");
```

redis-cli

```console
redis> GET global:foo
"\"var\""
redis> GET global:obj
"{\"key\":\"value\"}"
redis>
```

---
_Source: https://npm.io/package/@neohelden/node-red-context-redis · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
