I'm trying to find a way to do what is shown in the diagram below with Node.js. Is it even possible?
Basically, I need to put an http request to wait for a signal from another http request in order to finish.
I'm trying to find a way to do what is shown in the diagram below with Node.js. Is it even possible?
Basically, I need to put an http request to wait for a signal from another http request in order to finish.
According to this you can provide a callback to an http request which you could use to finish the request to your server.
My two cents; try the following code, although it's not properly structured it should do the trick. You can call /first in different tabs multiple times and refresh /second to see them responding one by one.
var http = require('http'),
Promise = require('promise');
var queue = [];
function handleRequest(request, response) {
switch (request.url) {
case "/first":
new Promise(function (resolve) {
var guid = 'some-guid';
// enqueue in redis or whatever you like and save the guid to match later.
queue.push({
data: guid,
resolve: resolve
});
}).then(function () {
response.end('1st finished');
});
break;
case "/second":
// queue consumer calls this (probably with the guid parameter)
queue.length && queue.some(function (item) {
if (item.data === 'some-guid') {
item.resolve();
}
});
response.end();
break;
default:
response.end('call /first, then /second');
break;
}
}
var server = http.createServer(handleRequest);
server.listen(8080, function () {
console.log("Server listening on: http://localhost:%s", 8080);
});