High performant advanced Redis session manager for NodeJS.
Redisess requires "ioredis" library to work. It has been tested for redis, node-redis and ioredis client libraries.
$ npm install ioredis redisess --save
The example blow show how can you use Redisess in a simple express applicaiton.
import express from 'express';
import Redis from 'ioredis';
import {SessionManager} from 'redisess';
const client = new Redis();
const manager = new SessionManager(client, {
namespace: 'myapp',
additionalFields: ['groupId'],
ttl: 120 // Default Time-To-Live value in seconds: 120 seconds
});
const app = express();
app.get('/login', async function (req, res) {
const userName = req.query.userName;
const pass = req.query.password;
//...Login application logic here
const session = await sm.create(userName, {
ttl: 240, // You can overwrite ttl value per session
groupId: 111 // You can store additional values
});
res.send('Your session id is '+session.sessionId);
});
app.get('/killSession/:sessionid', async function (req, res) {
await sm.kill(req.params.sessionid);
res.send('Session ' + req.params.sessionid + ' is closed');
});
app.get('/killUser/:userId', async function (req, res) {
await sm.killUser(req.params.userId);
res.send('All sessions for user "' + req.params.userId +'" are closed.');
})
app.listen(3000);
Returns the number of sessions within the last n seconds. Get all session count if n is not defined or zero
count(secs: number = 0): Promise<number>
- secs: The elapsed time since the last activity of the session. Returns total count of sessions If not defined or zero
- Return value : Returns the number of sessions.
Retrieves session count of single user which were active within the last n seconds.
countForUser(userId: string, secs: number = 0): Promise<number>
- userId: Id of the user
- secs: The elapsed time since the last activity of the session. Returns total count of sessions If not defined or zero
- Return value : Returns the number of sessions.
Creates a new session for the user
create(userId: string, props?: { ttl?: number, [index: string]: any }): Promise<Session>
- userId: Id of the user
- props: Additional properties
- ttl: Time-To-Live value in seconds
- *...: Additional fields
- Return value : Returns new created session.
Retrieves session by sessionId
get(sessionId: string, noUpdate: boolean = false): Promise<Session>
- sessionId: Id of the session
- noUpdate: Update state of the session
- Return value : Returns new created session.
Retrieves all session ids which were active within the last n seconds.
getAllSessions(secs: number): Promise<string[]>
- secs: The elapsed time since the last activity of the session. Returns total count of sessions If not defined or zero
- Return value : Returns the string array of all sessions.
Retrieves all user ids which were active within the last n seconds.
getAllUsers(secs: number): Promise<string[]>
- secs: The elapsed time since the last activity of the session. Returns total count of sessions If not defined or zero
- Return value : Returns the string array of all users.
Retrieves session ids of single user which were active within the last n seconds.
getUserSessions(userId: string, n: number = 0): Promise<string[]>
- userId: Id of the user
- n: The elapsed time since the last activity of the session.
- Return value : Returns the string array of all sessions for an user.
Retrieves oldest session of user
getOldestUserSession(userId: string, noUpdate: boolean = false): Promise<Session>
- userId: Id of the user
- noUpdate: Update state of the session
- Return value : Returns new created session.
Returns true if sessionId exists, false otherwise
exists(sessionId: string): Promise<Boolean>
- sessionId: Id of the session
- Return value : Returns Boolean.
Kills single session
kill(sessionId: string): Promise<void>
- sessionId: Id of the session
- Return value : No return value.
Kills all sessions of user
killUser(userId: string): Promise<void>
- userId: Id of the user
- Return value : No return value.
Kills all sessions for application
killAll(): Promise<void>
- No parameter value
- Return value : No return value.
Retrieves present time.
now(): Promise<number>
- No parameter value
- Return value : Returns number.
Stops wipe timer
quit(): void
- No parameter value
- Return value : No return value.
Returns session id value
sessionId(): string
Returns user id value
userId(): string
Returns Time-To-Live value
ttl(): number
Returns the time (unix) of last access
lastAccess(): number
Returns the time (unix) that session be expired.
expires(): number
Returns duration that session be expired.
expiresIn(): number
Returns validation of session and user with last access control.
valid(): boolean
Returns idle duration in seconds.
idle(): number
Returns any additional field value
Reads session info from redis server
read(): Promise<void>
Retrieves user data from session.
get(key): Promise<any>
- key: string | Array | Object<String,*>
- Return value : No return value.
Stores user data to session
set(key, value): Promise<number>
- key: string | Object
- value: *
- Return value : Length of values.
Kills the session
kill(): Promise<void>
Write session to redis server.
write(): Promise<void>
- node >= 8.x
To see changelog click here
Available under MIT license.