blog image

Using Redis with Node.js

Redis is an in-memory, JSON-like database/cache that is used for numerous purposes.

  1. A Redis cluster offers, besides redundancy, a unique way to share information.
  2. In my implentation, it is used to share data between node.js routes and events.
  3. Data that are processed in a route can be accessed within a emit, otherwise inaccessible across technologies.
  4. Redis is good for more than that - actually there are many use-cases, including session management, pkg delivery, etc.
I use npm, the Node Package Manager, so to install ioredis and

    npm i ioredis

I am using redis 6 and 4

To use redis with in Node.js, require them in app.js:

    const Redis = require('ioredis');
    const redis = new Redis();

    var https = require('https');
    const { Server } = require('');

    var port = 3000;
    app.set('port', port);

    const options = {
        key     : fs.readFileSync(certpath + 'privkey.pem'),
        cert    : fs.readFileSync(certpath + 'fullchain.pem')

    const httpServer = https.createServer(options, app);
    const io = new Server(httpServer);
    httpServer.listen(port, () => {
        console.log(`${} listening on ${port}`);
To start our https server using the pm2 module go:
    pm2 start app.js

in the route:

        res.locals.member = req.authenticated.user;
        redis.set('member', res.locals.member);
    } else {
        res.locals.member = '';
        redis.set('member', '');
and in
    io.on('connection', (socket) => {
        socket.on('member', (data) => {
            var member = redis.get('member');
            socket.emit('member', 'You are logged in as ' + member);

This is a pretty simple implementation, but you get the idea.

Redis is open-source and is used by millions of programmers world-wide.

If you would like to use Redis in your next project, visit

Leave a comment

About Me

I have been developing web sites since 1999. The knowledge and experience I have attained over the years makes me well suited to serve your web development needs, whatever they may be.

I am a husband and we have two awesome adult kids, with one in college (ASU, Pop Music) and a Creative Writer (MFA).

I love what I do and I love my clients. I look forward to serving you!

Contact Me

Your interest and/or feedback is important to me. Please use this form or call if it is urgent. Thank you!


Scottsdale, AZ



Make a Payment

This form will take you to PayPal. Please use this form to pay with your credit card. Thank you!

Note: You are making a payment to Web Renegade


Thank you for your business!