Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are you supposed to leave Redis open, or open and quit it after each use in node?

I have a socket.io server using redis called "server.js" that fires up a node server. Currently it is something like this:

var client = redis.createClient()
var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  client.set(); // do something with redis
});

Then I fire up my server and it just stays alive. Is this wrong? Should it be like this?

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  var client = redis.createClient()
  client.set(); // do something with redis
  client.quit();
});

Am I supposed to keep opening and closing redis, or can I just open it once and leave it open? Which one of the above snippets is the proper way to start a server?

like image 314
kidcapital Avatar asked Dec 13 '11 22:12

kidcapital


People also ask

How do I use Redis in node JS?

Create new session. js file in the root directory with the following content: const express = require('express'); const session = require('express-session'); const redis = require('redis'); const client = redis. createClient(); const redisStore = require('connect-redis')(session); const app = express(); app.

What is Redis connection timeout?

The maximum length of time to wait while establishing a connection to a Redis server.

How many connections can Redis handle?

Large number of connections Individual ElastiCache for Redis nodes support up to 65,000 concurrent client connections.


1 Answers

The first one is the preffered syntax because you don't want to make a new redis connection each time a clients connects to Socket.IO. If you have 1000 users connected would you want to have 1000 connections to Redis or just one (ok maybe more since you'd spawn more servers)?

As @racar suggested, you should take a look also at this question:

How to reuse redis connection in socket.io?

like image 171
alessioalex Avatar answered Sep 27 '22 21:09

alessioalex