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?
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.
The maximum length of time to wait while establishing a connection to a Redis server.
Large number of connections Individual ElastiCache for Redis nodes support up to 65,000 concurrent client connections.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With