A
A
Alexander Litvinov2014-08-10 14:08:03
Redis
Alexander Litvinov, 2014-08-10 14:08:03

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();
        });

    });

And something like this looks like the code in another part of the application.
subscribe.on("subscribe", function (channel, count) {
           redisClient.publish('cool ch', "I am sending a message.");
 });

But in order for it to work, you need to somehow pass the subscribe object

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Pavlov, 2014-08-10
@Sander_Li

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 question

Ask a Question

731 491 924 answers to any question