Redisess
High performant advanced Redis session manager for NodeJS.
Installation
Redisess requires a redis client library to work.
It has been tested for redis,
node-redis and
ioredis client libraries.
$ npm install redis redisess --save
or
$ npm install ioredis redisess --save
or
$ npm install node-redis redisess --save
Basic Usage
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
});
const app = express();
app.get('/login', async function (req, res) {
const userName = req.query.userName;
const pass = req.query.password;
const session = await sm.create(userName, {
ttl: 240,
groupId: 111
});
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);
SessionManager
prototype.count()
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>
Parameters
- 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.
prototype.countForUser()
Retrieves session count of single user which were active within the last n seconds.
countForUser(userId: string, secs: number = 0): Promise<number>
Parameters
- 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.
prototype.create()
Creates a new session for the user
create(userId: string, props?: { ttl?: number, [index: string]: any }): Promise<Session>
Parameters
- userId: Id of the user
- props: Additional properties
- ttl: Time-To-Live value in seconds
- *...: Additional fields
- Return value : Returns new created session.
prototype.get()
Retrieves session by sessionId
get(sessionId: string, noUpdate: boolean = false): Promise<Session>
Parameters
- sessionId: Id of the session
- noUpdate: Update state of the session
- Return value : Returns new created session.
prototype.getAllSessions()
Retrieves all session ids which were active within the last n seconds.
getAllSessions(secs: number): Promise<string[]>
Parameters
- 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.
prototype.getAllUsers()
Retrieves all user ids which were active within the last n seconds.
getAllUsers(secs: number): Promise<string[]>
Parameters
- 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.
prototype.getUserSessions()
Retrieves session ids of single user which were active within the last n seconds.
getUserSessions(userId: string, n: number = 0): Promise<string[]>
Parameters
- 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.
prototype.getOldestUserSession()
Retrieves oldest session of user
getOldestUserSession(userId: string, noUpdate: boolean = false): Promise<Session>
Parameters
- userId: Id of the user
- noUpdate: Update state of the session
- Return value : Returns new created session.
prototype.exists()
Returns true if sessionId exists, false otherwise
exists(sessionId: string): Promise<Boolean>
Parameters
- sessionId: Id of the session
- Return value : Returns Boolean.
prototype.kill()
Kills single session
kill(sessionId: string): Promise<void>
Parameters
- sessionId: Id of the session
- Return value : No return value.
prototype.killUser()
Kills all sessions of user
killUser(userId: string): Promise<void>
Parameters
- userId: Id of the user
- Return value : No return value.
prototype.killAll()
Kills all sessions for application
killAll(): Promise<void>
Parameters
- No parameter value
- Return value : No return value.
prototype.now()
Retrieves present time.
now(): Promise<number>
Parameters
- No parameter value
- Return value : Returns number.
prototype.quit()
Stops wipe timer
quit(): void
Parameters
- No parameter value
- Return value : No return value.
Session
prototype.sessionId
Returns session id value
sessionId(): string
prototype.userId
Returns user id value
userId(): string
prototype.ttl
Returns Time-To-Live value
ttl(): number
prototype.lastAccess
Returns the time (unix) of last access
lastAccess(): number
prototype.expires
Returns the time (unix) that session be expired.
expires(): number
prototype.expiresIn
Returns duration that session be expired.
expiresIn(): number
prototype.valid
Returns validation of session and user with last access control.
valid(): boolean
prototype.idle
Returns idle duration in seconds.
idle(): number
prototype.[additionalField]
Returns any additional field value
prototype.read()
Reads session info from redis server
read(): Promise<void>
prototype.get()
Retrieves user data from session.
get(key): Promise<any>
Parameters
- key: string | Array | Object<String,*>
- Return value : No return value.
prototype.set()
Stores user data to session
set(key, value): Promise<number>
Parameters
- key: string | Object
- value: *
- Return value : Length of values.
prototype.kill()
Kills the session
kill(): Promise<void>
prototype.write()
Write session to redis server.
write(): Promise<void>
Node Compatibility
Change log
To see changelog click here
License
Available under MIT license.