Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does hapi js uses redis to share session?

Tags:

redis

hapi.js

Redis is commonly used by expressJS to share session across multiple node js processes. Does hapiJs use similar technique?

like image 670
JustWonder Avatar asked Dec 24 '15 15:12

JustWonder


People also ask

How to use redis for session management?

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.

What is redis used for in Node JS?

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.

What is Hapi in Nodejs?

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.


1 Answers

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.

1. Background

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

2. Using Redis with Yar and Server-side storage

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');
    });
});
like image 148
Matt Harrison Avatar answered Oct 06 '22 20:10

Matt Harrison