Redis is commonly used by expressJS to share session across multiple node js processes. Does hapiJs use similar technique?
To add support of Redis you have to use Redis client and connect-redis. Create express-session and pass it to connect-redis object as parameter. This will initialize it. Then in session middleware, pass the Redis store information such as host, port and other required parameters.
Redis, an in-memory database that stores data in the server memory, is a popular tool to cache data. You can connect to Redis in Node. js using the node-redis module, which gives you methods to retrieve and store data in Redis.
Hapi is a rich framework for building applications and services. It enables developers to focus on writing reusable application logic instead of spending time building infrastructure. Several organizations such as Paypal, Vendigo, Clarify, Pling, and Npm already use Hapi in production.
Before I answer your question, I want to give a little background. Yes, you can use Redis for sharing sessions but first be sure it's what you really want/need Things are a bit different with hapi and Express when it comes to sessions.
If you've already decided this is the setup you want, skip to part 2.
In Express, the only thing stored in your cookie is your session ID. This means when you need to scale beyond a single server, you need to look at sharing sessions between them somehow. Redis is one option.
Note Session data is not saved in the cookie itself, just the session ID. Session data is stored server-side.
https://github.com/expressjs/session#sessionoptions
The official session plugin for hapi Yar, takes a slightly different approach than Express. As long as the data that you're trying to store in a session is below maxCookieSize
, the entire session will be encrypted (via Iron) and stored in a cookie. This means you don't need any extra machinery server-side to scale horizontally because the client remembers everything.
However, if this doesn't fit your requirements or you just don't like it, you can force server-side storage by setting Yar's maxCookieSize
to 0
.
maxCookieSize
- maximum cookie size before using server-side storage. Defaults to 1K. Set to zero to always use server-side storage.https://github.com/hapijs/yar/blob/master/API.md
When using Yar with server-side storage, hapi will use its cache functionality to store your data. To use Redis for example, just pass along a Redis cache
configuration to Yar:
const Hapi = require('hapi');
const Hoek = require('hoek');
const server = new Hapi.Server({
cache: {
engine: require('catbox-redis'),
name: 'session', // cache name
host: '127.0.0.1', // redis host
port: 6379, // redis port
database: 'my-db' // redis-db
}
});
server.register({
register: require('yar'),
options: {
maxCookieSize: 0, // force server-side storage
cache: {
cache: 'session'
},
cookieOptions: {
password: 'U32KuiKPnVguRKM', // cookie password
isSecure: false // allow non HTTPS
}
}
}, (err) => {
Hoek.assert(!err, err);
server.start((err) => {
Hoek.assert(!err, err);
console.log('Started server');
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With