Reputation: 345
We are currently restructuring one of our largest projects. The whole project is set up in an Node.js / PHP mixed envorinment and is quite distributed. This is why we are planning to use queueing for various services. We are extensively using Redis in this system, so I came across RSMQ (http://smrchy.github.io/rsmq/) which I am trying to implement. My question is: How do I actually implement a worker task or similar using this queue? As far as I understand it, it's quite similiar to SQS without any overhead - which I like, but I am not quite sure how to really get work done with it.
So far my approach (simplified) looks like this:
RQ = require("rsmq");
queue = new RQ( {host: "127.0.0.1", port: 6379, ns: "nDispatch"} );
queue.receiveMessage({qname:"mq"}, function() {
// code here
});
But the question is - do I have to poll the queue manually? Is there no message event or similar?
Upvotes: 2
Views: 1036
Reputation: 687
RSMQ itself does not implement any kind of worker, and ist stripped down to the core functionalities of a queue (which in my opinion is the correct approach). While you could implement everything yourself of course, I'd recommend using the additional module rsmq-worker (http://smrchy.github.io/rsmq/rsmq-worker/) which provides the basic worker skeleton for your application.
They provide a simple but usable example on their site:
var RSMQWorker = require( "rsmq-worker" );
var worker = new RSMQWorker( "myqueue" );
worker.on( "message", function( msg, next ){
// process your message
next()
});
// optional error listeners
worker.on('error', function( err, msg ){
console.log( "ERROR", err, msg.id );
});
worker.on('exceeded', function( msg ){
console.log( "EXCEEDED", msg.id );
});
worker.on('timeout', function( msg ){
console.log( "TIMEOUT", msg.id, msg.rc );
});
worker.start();
I have used and implemented it this way and it works really well. The worker does the polling for you and offers error events which should be enough to get you going.
Upvotes: 4