Latest: Genstatic, my first sip of coffee

Content with Style

Web Technique

Nodestalker - A beanstalkd client for node.js

by Pascal Opitz on May 14 2010, 12:31

Following my previous article about PHP and beanstalkd I was keen to use beanstalk in combination with node.js, but wasn't entirely satisfied with the available implementation. So as a little brain exercise I wrote nodestalker from scratch.


Usage should be pretty straight forward. Below is the code for putting a job into the queue:

var sys = require('sys');
var bs = require('../lib/beanstalk_client');
var client = bs.Client();

client.use('default').onSuccess(function(data) {

  client.put('my job').onSuccess(function(data) {

Issues during development

Writing it wasn't as easy as I expected, but that was because I didn't read the documentation on sockets properly, and missed out on stream.setKeepAlive() and stream.setNoDelay() first, which made my sockets close on me in what I thought was a random manner.

Another issue I had was the yaml library for node, which just didn't like the yaml formatted beanstalk output, which doesn't contain the expected indentations. Not sure why, maybe a yaml version conflict or something, but I hacked the yaml parsing with some regular expressions.

var corrected = str.replace(/\n-\ ([\w\d_-]+)/mig, '\n  - \'$1\'') //indent list
          .replace(/(\w)\-(\w)/mgi, '$1_$2') // replace minuses in hash names
          .replace(/\n([\w\d_-]+)\:\ ([\.\,\w\d_-]+)/mig, '\n  $1: \'$2\''); // format hashes
try {
  return kiwi.require('yaml').eval(corrected);
} catch(e) {
  return str;

Demo application: Beanspector

I also included a demo application, which I baptised "beanspector". It is a simple command line client to inspect, list and empty tubes and put some content into them. This is how I use it in action:

$ node beanspector.js -lt
[ 'default', 'confirm' ]
$ node beanspector.js -pt default "here"
$ node beanspector.js -lc default
listing tube default
{ id: '61', data: 'here' }


Get nodestalker from GitHub, and feel free to fork and do some improvements if you want.


  • I rolled out a bunch of updates a couple of days ago and moved nodestalker into npm. Beanspector now is it's own repo on github.

    by Pascal Opitz on March 6 2011, 06:49 - #

Comments for this article are closed.