Home

Awesome

Redis Sessions

Redis-Sessions

This is a Node.js module to keep sessions in a Redis datastore and add some useful methods.

The main purpose of this module is to generalize sessions across application server platforms. We use nginx reverse proxy to route parts of a website to a Node.js server and other parts could be Python, Ruby, .net, PHP, Coldfusion or Java servers. You can then use rest-sessions (incompatible with version 4.0.0) to access the same sessions on all app servers via a simple REST interface.

If you use Express check out Connect-Redis-Sessions (incompatible with version 4.0.0) for a ready to use middleware.

!!! BREAKING CHANGES VERSION 4.0 !!!

Due to a change from callbacks to async/await, version 4.0.0 is incompatible with version 3.x or lower.
Migration-Guide.

connect-redis-sessions and rest-sessions are both incompatible with version 4.0.0.

Installation

npm install redis-sessions

Basics

Additional methods

Performance

With Redis running on the same machine as the test script (run via npm test) on a 2018 MacBook Pro:

Cache (optional setting)

Modern apps might also use a lot of requests while a user is active. This results in a lot of Redis requests to look up sessions. What's faster than an in-memory cache in Redis? An in-memory cache right in your app! When you enable caching you can speed up session lookups by a lot. Consider the following before you enable it:

How does the cache work

What would be a good value for the cachetime option?

If your sessions last for 24h and the average user-session is 20m. You might as well set the cachetime to around 30m. Consider the size of your session object that has to be kept in memory. Setting the cachetime lower is ok. Because after all it just takes a quick Redis request to fill your cache again.

Use via REST (This is currently not compatible with the latest version)

See rest-sessions (incompatible with version 4.0.0).

Use in Node.js

Initialize redis-sessions

import RedisSessions from "redis-sessions"
//
// Parameters for RedisSession:
//
// e.g. rs = new RedisSession({host:"192.168.0.20"});
//
// `port`: *optional* Default: `6379`. The Redis port.
// `host`, *optional* Default: `127.0.0.1`. The Redis host.
// `options`, *optional* Default: {}. Additional options. See: https://github.com/redis/node-redis/blob/master/docs/client-configuration.md
// `namespace`: *optional* Default: `rs`. The namespace prefix for all Redis keys used by this module.
// `wipe`: *optional* Default: `600`. The interval in seconds after which expired sessions are wiped. Only values `0` or greater than `10` allowed. Set to `0` to disable.
// `cachemax` (Number) *optional* Default: `5000`. Maximum number of sessions stored in the cache.
rs = new RedisSessions<{
  foo: string;
  unread_msg?: number;
  last_action?: string;
  birthday?: string;
}>();

rsapp = "myapp";

Create a session

Parameters:


// Set a session for `user1001`

const resp = await rs.create({
  app: rsapp,
  id: "user1001",
  ip: "192.168.22.58",
  ttl: 3600,
  d: { 
    foo: "bar",
    unread_msgs: 34
  }
  });
  // resp should be something like 
  // {token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe"}

Notes:

Update and add some more data to an existing session

const resp = await rs.set({
  app: rsapp,
  token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe",
  d: {
    unread_msgs: 12,
    last_action: "/read/news",
    birthday: "2013-08-13"
  }});
  /*
  resp contains the session with the new values:

  {
    "id":"user1001",
    "r": 1,
    "w": 2,
    "idle": 1,
    "ttl": 7200, 
    "d":{
      "foo": "bar",
      "unread_msgs": 12,
      "last_action": "/read/news",
      "birthday": "2013-08-13"
    }
  }
  */

Note: The key foo that we didn't supply in the set command will not be touched. See Set/Update/Delete details for details on how to remove keys.

Get a session for a token

const resp= await rs.get({
  app: rsapp,
  token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe"});
  /*
  resp contains the session:

  {  
    "id":"user1001",
    "r": 2,  // The number of reads on this token
    "w": 2,  // The number of writes on this token
    "idle": 21,  // The idle time in seconds.
    "ttl": 7200, // Timeout after 7200 seconds idle time
    "d":{
      "foo": "bar",
      "unread_msgs": 12,
      "last_action": "/read/news",
      "birthday": "2013-08-13"
    }
  }

  */

Set/Update/Delete

Set/Update/Delete parameters by supplying app, token and some data d.
The d object contains a simple key/value list where values
can be string, number, boolean or null.
To remove keys set them to null, keys that are not supplied will not be touched.


const resp = await rs.set({
  app: rsapp,
  token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe",
  d: {
      "unread_msgs": null
      "last_action": "/read/msg/2121"
  }});
  /*
  resp contains the session with modified values:

  {
    "id":"user1001",
    "r": 2,
    "w": 3,
    "idle": 1,
    "ttl": 7200, 
    "d":{
      "last_action": "/read/msg/2121",
      "birthday": "2013-08-13",
      "foo": "bar"
    }
  }
  */

Kill

Kill a single session by supplying app and token:


const resp = await rs.kill({
  app: rsapp,
  token: "r30kKwv3sA6ExrJ9OmLSm4Wo3nt9MQA1yG94wn6ByFbNrVWhcwAyOM7Zhfxqh8fe"});
  /*
  resp contains the result:

  {kill: 1}
  */

Note: If {kill: 0} is returned the session was not found.

Activity

Query the amount of active session within the last 10 minutes (600 seconds). Note: Multiple sessions from the same user id will be counted as one.


const resp = await rs.activity({
  app: rsapp,
  deltaTime: 600
  });
  /*
  resp contains the activity:

  {activity: 12}
  */

Sessions of App

Get all sessions of an app there were active within the last 10 minutes (600 seconds).


const resp = await rs.soapp({
  app: rsapp,
  deltaTime: 600
  });
  /*
  resp contains the sessions:

  {
    sessions: [
      {
        id: 'someuser123',
        r: 1,
        w: 1,
        ttl: 30,
        idle: 0,
        ip: '127.0.0.2'
      },
      {
        id: 'anotheruser456',
        r: 4,
        w: 2,
        ttl: 7200,
        idle: 24,
        ip: '127.0.0.1'
      }
    ]
  }
  */

Sessions of Id

Get all sessions within an app that belong to a single id. This would be all sessions of a single user in case he is logged in on different browsers / devices.


const resp = await rs.soid({
  app: rsapp,
  id: "bulkuser_999"
  });
  /*
  resp contains the sessions:

  {
    sessions: [
      {
        id: 'bulkuser_999',
        r: 1,
        w: 1,
        ttl: 30,
        idle: 0,
        ip: '127.0.0.2'
      },
      {
        id: 'bulkuser_999',
        r: 1,
        w: 1,
        ttl: 7200,
        idle: 0,
        ip: '127.0.0.1'
      }
    ]
  }
  */

Kill all sessions of an id

Kill all sessions of an id within an app:


const resp = rs.killsoid({app: rsapp, id: 'bulkuser_999'});
  /*
  resp contains the result:

  {kill: 2} // The amount of sessions that were killed
  */

Killall

Kill all sessions of an app:


const resp = await rs.killall({app: rsapp});
  /*
  resp contains the result:

  {kill: 12} // The amount of sessions that were killed
  */

Ping

Ping the redis server


const resp = await rs.ping();
  /*
  resp contains the result:

  "PONG"
  */

Tests

Before running Test you need to build the js files (npm run build) and have a redis server running.

Typescript Pitfalls !!!

CHANGELOG

See CHANGELOG.md

More Node.js and Redis projects?

Check out my projects which are based on Node.js and Redis as a datastore:

RSMQ: Really Simple Message Queue

If you run a Redis server and currently use Amazon SQS or a similar message queue you might as well use this fast little replacement. Using a shared Redis server multiple Node.js processes can send / receive messages.

Redis-Tagging

A Node.js helper library to make tagging of items in any legacy database (SQL or NoSQL) easy and fast. Redis is used to store tag-item associations and to allow fast queries and paging over large sets of tagged items.

The MIT License (MIT)

Copyright © 2013 Patrick Liess, http://www.tcs.de

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.