mmatey
mmatey

Reputation: 83

Async call with Express and Redis

I'm working with Redis Pub/Sub to transport messages from Node.js to a backend server.

The issue is how to get the message from the subscriber message event to be accessible in the express response. Is this something I should take care of with middleware? Or is there an easier way with an async callback?

Here's a basic example of what I'm talking about:

subscriber.on("message", function (channel, message) {
    console.log("received: " + channel + ": " + message);
});

server.get('/', function(req, res){
    publisher.publish(server.uuid, server.uuid + ": message here");
    res.send(message);
});

Update:

Solved my own problem by passing the response object with an id to a async queue.

Upvotes: 2

Views: 1366

Answers (1)

Lloyd Moore
Lloyd Moore

Reputation: 3197

I have only a tiny amount of experience with node.js, however something similar to the following will work. I think you simply need to hit the server with a request that contains the message in a pattern that the server recognises. In the following example the request is http://yoursite.com/msg/some_msg_here.

var http = require('http');
var site = http.createClient('127.0.0.1', 80);

subscriber.on("message", function(channel, message)) {
    var req = site.request("GET", "/msg/" + message, {'host': '127.0.0.1'});
    req.end();
}

server.get('/msg', function(req, res){
    var msg = req.params.msg;
    publisher.publish(server.uuid, server.uuid + ": " + msg;
    res.send(msg);

Hope that helps as a starting point if not a final solution.

Upvotes: 2

Related Questions