Node.js – client.end Method in Redis


The client.end(flush) method forcibly closes all the connection to the Redis server without waiting until all the replies have been parsed. This method just closes all the connection and ongoing streaming between the Node and Redis server. If you want to exit cleanly, you should use client.quit() method.

Syntax

client.end(flush)

Parameters

  • flush - This input parameter will hold a Boolean value that will indicate whether to close a connection or not.

Example 1

Create a file with the name "clientEnd.js" and copy the following code. After creating the file, use the command "node clientEnd.js" to run this code as shown in the example below −

// client.end() Demo Example

// Importing the redis module
const redis = require("redis");

// Creating redis client
const client = redis.createClient();

client.set("Hello", "tutorialsPoint", function(err) {
   // This will return an error since client.end is set to true
   console.error(err);
});

// No further commands will be processed
client.end(true);
client.get("hello", function(err) {
   console.error(err); // The connection has already been closed.
});

Output

It will produce the following output −

C:\home
ode>> node clientEnd.js { AbortError: Connection forcefully ended and command aborted. at RedisClient.flush_and_error (/home/tutorialsPoint/example/node_modules/redis/index.js:298:23) at RedisClient.end (/home/tutorialsPoint/example/node_modules/redis/lib/extendedApi. js:52:14) at Object.<anonymous> (/home/tutorialsPoint/example/redis.js:19:8) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) code: 'NR_CLOSED', command: 'SET', args: [ 'Hello', 'tutorialsPoint' ] } { AbortError: Connection forcefully ended and command aborted. at RedisClient.flush_and_error (/home/tutorialsPoint/example/node_modules/redis/index.js:298:23) at RedisClient.end (/home/tutorialsPoint/example/node_modules/redis/lib/extendedApi. js:52:14) at Object.<anonymous> (/home/tutorialsPoint/example/redis.js:19:8) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) code: 'NR_CLOSED', command: 'GET', args: [ 'hello' ] }

Updated on: 24-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements