Node.js Kue worker send result

heroku, kue, node.js, worker

Solution

Here is the code I got to do what I think you're doing.

Simply this just adds a job when the url /job gets hit a worker job is created that the running worker picks up, waits a half a second and generates a random number. This is the non-socket.io version. You probably shouldn't do it like this, but it works.

The web server (web.js):

var port = 1337;
var app = require('express')()
  , server = require('http').createServer(app);
server.listen(port);
var fs = require('fs');
var kue = require('kue')
  , jobs = kue.createQueue();


app.get('/',function(req,res) {
    fs.readFile(__dirname+'/index.html',function(err,data) {
        res.send(data.toString());
    });
});

var responses = {};
app.get('/job', function(req, res){
    var id = ''+Math.random();
    jobs.create('worker-job', {
        title: 'worker job'
        , res_id: id
        , template: 'Some string to look up'
    }).save();
    responses[id] = res;
});

jobs.process('results',function(job,done) {
    responses[job.data.res_id].send(job.data);
    delete responses[job.data.res_id];
    done();
});

The worker (worker.js):

var kue = require('kue')
  , jobs = kue.createQueue();

jobs.process('worker-job',function(job,done) {
    setTimeout(function() {
        jobs.create('results',{
            title:'results job',
            status:'complete',
            res_id:job.data.res_id,
            random:Math.ceil(Math.random()*10000)
        }).save();
        done();
    },400);
});

Problem

My app makes use of Kue to queue up requests, which are handled by `worker.js` because I need to send the requests that the job makes through Proximo - it's a little confusing. But because of this, the results of the job are unable to be sent back to the user. Previously the job would `res.send(results)` and then the user would have the data. What would be the best way to get the Kue'ed job to send results back to the user? The only way I can think of at the moment is to use a web hook, but it's not the most efficient way of doing so and it builds a wall between the user and their data.

Original source