Search code examples
javascriptnsqpynsq

Is Nsqjs really slow compared to Pynsq?


I have the following code in javascript

var nsq = require('nsqjs');

var reader = new nsq.Reader('output', 'out', {
  lookupdHTTPAddresses: '172.32.10.224:4161'
});

reader.connect();


reader.on('message', function (msg) {
  console.log('Received message [%s]: %s', msg.id, msg.body.toString());
});

and here is the equivalent version in python.

import nsq

def handler(message):
    print str(message.body)
    return True

r = nsq.Reader(message_handler=handler,
        lookupd_http_addresses=['http://172.32.10.224:4161'],
        topic='output', channel='out')
nsq.run()

The python version runs extremely fast compared to javascript. The javascript version seems to process 1 message for every 2 mins. And python seems to process in milliseconds. I am really surprised what is going on here? This is the exact code I ran and my queue has plenty of message which I did verify. any ideas why javascript version is really slow?


Solution

  • You're not telling NSQ that you handled the message:

    reader.on('message', function (msg) {
      console.log('Received message [%s]: %s', msg.id, msg.body.toString());
      msg.finish(); // <---
    });
    

    See here.

    I think the Python driver does this implicitly based on the return value of the handler (True is finish, False is requeue).