kidcapital
kidcapital

Reputation: 5174

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?

Upvotes: 11

Views: 5855

Answers (1)

alessioalex
alessioalex

Reputation: 63673

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?

Upvotes: 2

Related Questions