Open In App

How to establish connection between Node.js ans Redis ?

Improve
Improve
Like Article
Like
Save
Share
Report

NodeJS is an open-source back-end JavaScript runtime environment for executing JavaScript outside the browser. It is widely used for server-side development in many companies. 

Redis is a popular in-memory key-value database. Unlike traditional databases that run on a computer’s hard disk and store all of their data on disk, Redis runs inside of a system’s working memory (RAM). This allows Redis to be incredibly fast at fetching data, which is why it’s often used as a cache on top of other databases to improve application performance.

In this tutorial, you will learn how to establish a connection between NodeJS and Redis using the node-redis library. The NodeJS and Redis must be installed on your machine in order to make a connection with the Redis.  Let’s understand how to make the connection of NodeJS to Redis.

Create a new NodeJS project: First, create a blank NodeJS project with npm init -y. In your working folder, make a file named index.js for NodeJS.

Installing Redis client in NodeJS project: To use Redis with NodeJS, you need to install a NodeJS Redis client. node-redis is the Redis client for Node. Install it with the following command:

npm install redis

Once you’ve installed the Redis module, you can access Redis in your NodeJS application. 

Create a Redis object: In your main server file, create a Redis object. The Redis module’s createclient() method creates a Redis object. 

const redis = require('redis')
const redisClient = redis.createClient()

The above code connects to the localhost on port 6379 by default. Use a connection string to your remote Redis DB to connect to a different host or port.

The connect() method is used to connect the NodeJS to the Redis Server. This return promise that’s why we have to handle it either using the then and catch or using the sync and await keyword.

(async () => {
    await redisclient.connect();
})();

Emitting events from Redis client: 

The client emits a ready event when it successfully initiates the connection to the server. Add an event handler for the ready event which outputs a message to the console if the client successfully connects:

redisClient.on('ready', () => {
    console.log("Connected!");
});

We will also listen for an error event and output the error to the console if the event is triggered using the following code:

redisClient.on('error', (err) => {
    console.error(err);
});

index.js




const redis = require("redis");
const redisclient = redis.createClient();
  
(async () => {
    await redisclient.connect();
})();
  
console.log("Connecting to the Redis");
  
redisclient.on("ready", () => {
    console.log("Connected!");
});
  
redisclient.on("error", (err) => {
    console.log("Error in the Connection");
});


Run Redis-server: Before you run your Node project, make sure you are running the Redis server in a separate Terminal. You can launch a Redis server with the following command:

redis-server

Starting the Redis Server:

 

Execute the index.js file using the below command:

node index.js

Console Output:

 


Last Updated : 08 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads