Reputation: 69
I am working on to integrate redis, @socket.io/redis-adapter, socket.io and express-session. The following is the code :
// Promisify the methods so we can use async / await with redis
bluebird.promisifyAll(redis);
const redisClient = redis.createClient({ legacyMode: true });
const subClient = redisClient.duplicate({ legacyMode: true });
bluebird.promisifyAll(redisClient);
bluebird.promisifyAll(subClient);
const redisStore = new RedisStore({ client: redisClient });
// Applying Redis middleware
app.use(
session({
secret: process.env.SESSION_SECRET,
// create new redis store.
store: redisStore,
cookie: {
secure: !isDevEnv, // if true: only transmit cookie over https
httpOnly: true, // if true: prevents client side JS from reading the cookie
maxAge: 30 * 24 * 60 * 60 * 1000, // session max age in milliseconds
},
resave: false,
saveUninitialized: true,
rolling: true,
}),
);
In the below code redisClient
and subClient
are used to pass into createAdapter function . I am using redisClient
in another function called IdentityCheck
where we use that function to maintain single session for a user and we set the visitorId ( user id) using redis set and get function inside the function IdentityCheck
// Start the server
const server = app.listen(PORT, console.log(`App listening on port: ${PORT}`));
server.setTimeout(6 * 60 * 1000);
// Start socket io with cors enabled
const io = new Server();
io.adapter(adapter.createAdapter(redisClient, subClient));
(async () => {
Promise.allSettled([redisClient.connect(), subClient.connect()]).then(() => {
io.listen(server, {
cors: {
origin: [
'http://localhost:3000'
],
},
transports: ['websocket'],
});
identityCheck.init(io, redisClient); // using redisClient in other file where we use redis get, set methods
redisClient.on('error', (error) => console.error('error from redisClient', error));
subClient.on('error', (error) => console.error('error from subClient', error));
});
})();
Even though I set the error handler as following:
redisClient.on('error', (error) => console.error('error from redisClient', error));
subClient.on('error', (error) => console.error('error from subClient', error));
I still get the missing 'error' handler on this Redis client
. Am I doing anything wrong here. I am using the following versions :
{
"express": "^4.17.1",
"express-session": "^1.17.1",
"redis": "4.5.1",
"socket.io": "4.5.4",
"socket.io-client": "4.5.4",
"@socket.io/redis-adapter": "8.0.1",
}
redis version 4.5.1, socket.io, socket.io-client-4.5.4 and @socket.io/redis-adapter": "8.0.1",
Upvotes: 0
Views: 436
Reputation: 69
I downgraded the redis version to 3.1.2 and the error has gone! I think there's something to do with redis version 4 . If anyone has correct reason please feel free to answer. thanks
Upvotes: 0