Answer the question
In order to leave comments, you need to log in
What is the best way to pass an object to expres.js?
Good afternoon! You need to check the subscribe to redis in different parts of the application. What is the best way to implement this? Should I use Dependency Injection ?
The code looks like this
var redis = require("redis");
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3001);
io.on('connection', function (socket) {
const subscribe = redis.createClient();
subscribe.subscribe('cool ch');
subscribe.on("message", function (channel, message) {
socket.emit('cool ch', message);
});
socket.on('message', function (msg) {
});
socket.on('disconnect', function () {
subscribe.quit();
});
});
subscribe.on("subscribe", function (channel, count) {
redisClient.publish('cool ch', "I am sending a message.");
});
Answer the question
In order to leave comments, you need to log in
Do you have many different subscribers? Is there a separate one for each client?
Maybe it's better to pass the subscribe object from there to the initialization of io?
Or not to transfer, but to create a new subscribe on the spot. This is Redis - join the channel and listen.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question