Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I did some research on this and I would do it like this.</p> <h1>Setup</h1> <h2>beanstalkd</h2> <ol> <li><p>Install <a href="http://kr.github.com/beanstalkd/" rel="noreferrer">beanstalkd</a>. Another message queue, <strong>BUT</strong> this one supports <a href="https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt" rel="noreferrer">DELAYED PUTS</a>. If you compile from source it is going to be a little harder because it depends on <a href="http://monkey.org/~provos/libevent/" rel="noreferrer">libevent</a>(like memcached). But then again, I don't think you have to compile it from source, because there are a lot of <a href="http://kr.github.com/beanstalkd/download.html" rel="noreferrer">binary packages</a> available. For example on Ubuntu you can install beanstalkd by issuing the command:</p> <p><code>sudo apt-get install beanstalkd</code></p></li> </ol> <h2>node-beanstalk-client</h2> <ol start="2"> <li><p>Install a beanstalkd client library. The best one I found was <a href="https://github.com/benlund/node-beanstalk-client" rel="noreferrer">node-beanstalk-client</a>. Because on the beanstalkd client library list this library isn't/wasn't mentioned(Then again I can add entries to the list, so I will add this one). The reasons I prefer this library over the others are:</p> <ol> <li><strong>Npm package</strong>: I liked to use a npm package to install client library. The others did not have any.</li> <li><strong>Active development</strong>: I prefer libraries which have later/more commits.</li> </ol></li> </ol> <p>So to install it, after you have installed <a href="https://github.com/isaacs/npm" rel="noreferrer">npm</a>(<a href="https://gist.github.com/579814" rel="noreferrer">the write way</a>) you would just issue the following command:</p> <pre><code>npm install beanstalk_client </code></pre> <h1>Code</h1> <h2>consumer.js</h2> <pre><code>var client = require('beanstalk_client').Client; client.connect('127.0.0.1:11300', function(err, conn) { var reserve = function() { conn.reserve(function(err, job_id, job_json) { console.log('got job: ' + job_id); console.log('got job data: ' + job_json); console.log('module name is ' + JSON.parse(job_json).data.name); conn.destroy(job_id, function(err) { console.log('destroyed job'); reserve(); }); }); } reserve(); }); </code></pre> <p>First start the consumer: </p> <pre><code>node consumer.js </code></pre> <p>Next start <code>producer.js</code>. Five seconds(delay I specified) after you execute <code>producer.js</code>, <code>consumer.js</code> will process the message.</p> <h2>producer.js</h2> <pre><code>var client = require('beanstalk_client').Client; client.connect('127.0.0.1:11300', function(err, conn) { var job_data = {"data": {"name": "node-beanstalk-client"}}; var priority = 0; var delay = 5; var timeToRun = 1; conn.put(priority, delay, timeToRun, JSON.stringify(job_data), function(err, job_id) { console.log('put job: ' + job_id); process.exit(); }); }); </code></pre> <p>to start just issue:</p> <pre><code>node producer.js </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload