0

i want to use socket.io with cluster . This is what i tried but i got this error :

TypeError: RedisStore is not a constructor

const app = express()
const redis = require('redis')
var cluster = require('cluster'),
numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
//master process - fork children
for (var i = 0; i < numCPUs; i++) {
    cluster.fork()
}
}
else {
var RedisStore = require('redis')
    , pub = redis.createClient()
    , sub = redis.createClient()
    , client = redis.createClient();

var io = require('socket.io').listen(5000, {
    'store': new RedisStore({
        redisPub: pub
        , redisSub: sub
        , redisClient: client
    }),
});
io.sockets.on('connection', function (socket) {
    // all socket.on('eventname'... things go here
});
const http = require('http')
http.createServer(app).listen(4000);
}

1 Answer 1

2

RedisStore is a functionality added by the connect-redis module, not redis. to fix that you just need to change var RedisStore = require('redis') to var RedisStore = require('connect-redis').

RedisStore constructor expects to get an object with the redis property, and it should be a redis client (connect-redis API Docs).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks and does it need to install redis database ?
@soroushasamiesfahan the redis module is a client to work with a Redis database/server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.