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 socket.io events.
  3. Data that are processed in a route can be accessed within a socket.io 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 socket.io:

                            
    npm i ioredis socket.io
                            
                        

I am using redis 6 and Socket.io 4

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

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

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

    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(`${process.pid} listening on ${port}`);
    });
                        
                    
To start our https server using the pm2 module go:
                        
    pm2 start app.js
                        
                    

in the route:

                        
    if(req.authenticated.user){
        res.locals.member = req.authenticated.user;
        redis.set('member', res.locals.member);
    } else {
        res.locals.member = '';
        redis.set('member', '');
    }
                            
                        
and in socket.io:
                            
    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 Redis.io

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!

Location

Scottsdale, AZ

Phone

602-334-7771

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


Description:



Thank you for your business!