How to get number of connections in Redis using Node.js?

Viewed 1631

Is there a way to check how many current connections there are to the Redis database?

const redis = require('redis');

var client = redis.createClient(port, host);

client.on('connect', () => {
  //Display how many current connections are to Redis
  console.log( numberOfRedisClients );
});
2 Answers

according to redis documentation try this :

const Redis = require("ioredis");
let client =  new Redis();
async function my_function(){
  console.log(await client.send_command('CLIENT', 'LIST'))

}

the output :

id=24 addr=127.0.0.1:47962 fd=8 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=info
id=25 addr=127.0.0.1:47964 fd=9 name= age=0 idle=0 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=26 addr=127.0.0.1:47966 fd=10 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client

CLIENT LIST returns information and statistics about the client connections server in a mostly human readable format as @babak stated in the answer.

If you just need to get total number of connections you may use INFO command. INFO clients will print something like this and connected_clients would be what you are looking for.

# Clients
connected_clients:2
client_recent_max_input_buffer:8
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

The following code do the work;

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

async function clients() {
    console.log(await redis.info('clients'));
}

clients();
Related