The JavaScript blog.


events node servers pubsub


Posted on .


Autobahn|JS (GitHub: tavendo / AutobahnJS, License: MIT, npm: autobahn) is an implementation of a protocol that supports RPC and PubSub that runs over WebSocket, with fallbacks for older browsers.

The PubSub model supports topics, and uses a promise-based API:

// Subscribe to a topic
session.subscribe('com.myapp.topic1', onEvent).then(  
  function(subscription) {
    console.log('Subscribed:', subscription);
    currentSubscription = subscription;

  function(error) {
    console.error('Error:', error);

The support for RPC is based on a router, where routes are represented as strings. Registering a remote procedure also uses a promise:

// Define the remote procedure
function utcnow() {  
  var now = new Date();
  return now.toISOString();

// Register the remote procedure with the router
session.register('com.timeservice.now', utcnow).then(  
  function(registration) {
    console.log('Procedure registered:', registration.id);
  function(error) {
    console.error('Registration failed:', error);

You can call this remote procedure with session.call('com.timeservice.now'), which again returns a promise.

To use Autobahn, you also need an application router. The documentation mentions Crossbar.io. This allows you to create distributed server applications, so you could route between backends running on different platforms. Authorization is supported, so you can limit access to server-side resources.

The project is based on an open protocol called WAMP (Web Application Messaging Protocol). Other WAMP implementations include Autobahn|Android and wampy.js.


events cli node modules pubsub

Node Roundup: 0.11.2, 0.10.6, subscribe, Omelette

Posted on .

You can send in your Node projects for review through our contact form.

Node 0.11.2 and 0.10.6

Clearly the Node core developers have had an early summer holiday, and are now back to unleash new releases. In the space of a few days 0.11.2 and 0.10.6 were released. I was intrigued by the Readable.prototype.wrap update, which makes it support objectMode for streams that emit objects rather than strings or other data.

The 0.11.2 release has an update that guarantees the order of 'finish' events, and another that adds some new methods: cork and uncork. Corking basically forces buffering of all writes -- data will be flushed when uncork is called or when end is called.

There is a detailed discussion about cork and the related _writev method on the Node Google Group: Streams writev API. There are some interesting comments about a similar earlier implementation by Ryan Dahl, the validity of which Isaac questions due to Node's code changing significantly since then.

If you want to read about writev, check out the write(2) (man 2 write) manual page:

Write() attempts to write nbyte of data to the object referenced by the descriptor fildes from the buffer pointed to by buf. Writev() performs the same action, but gathers the output data from the iovcnt buffers specified by the members of the iov array...


Azer Koçulu has been working on a suite of modules for subscribing and observing to changes on objects:

Now he's also released a module for subscribing to multiple pub/sub objects:

var subscribe = require('subscribe');  
var a = pubsub();  
var b = pubsub();  
var c = pubsub();

subscribe(a, b, c, function(updates) {  
  // => a.onUpdate
  // => 3, 4
  // => c.onUpdate
  // => 5, 6

a.publish(3, 4);  
c.publish(5, 6);  

This brings a compositional style of working to Azer's other modules, allowing subscriptions to multiple lists and objects at the same time. The next example uses subscribe to combine the new-list module with new-object:

var fruits = newList('apple', 'banana', 'grapes');  
var people = newObject({ smith: 21, joe: 23 });

subscribe(fruits, people, function(updates) {  
  // => melon

  // => { alex: 25 }

people('alex', '25');  


Omelette (GitHub: f / omelette, License: MIT, npm: omelette) by Fatih Kadir Akın is a template-based autocompletion module.

Programs and their arguments are defined using an event-based completion API, and then they can generate the zsh or Bash completion rules. There's an animated gif in the readme that illustrates how it works in practice.


libraries JSON books node modules pubsub xml

Node Roundup: 0.8.9, xmlson, Mubsub, Book on libuv

Posted on .

You can send in your Node projects for review through our contact form or @dailyjs.

Node 0.8.9

Node 0.8.9 is out, and this looks like a significant release judging by the long changelog. v8, npm, and GYP have all been updated, and there are quite a few platform-specific bug fixes relating to memory.


xmlson (License: MIT, npm: xmlson) by the developers at Fractal is a libexpat-based XML/JSON conversion module:

var xmlson = require('xmlson');

xmlson.toJSON('<p><h1 title="Details">Title</h1></p>', function(err, obj) {  
  // Do something with obj

In the previous example, [ { '@title': 'Details', text: 'Title' } ] will be printed, so attributes are included when converting to JSON. There's also a synchronous API. Installing xmlson with npm will compile the necessary dependencies with gyp.

This module is a fairly lightweight wrapper around ltx, which is worth checking out.


Mubsub (License: MIT, npm: mubsub) by Scott Nelson is a publish–subscribe implementation that uses MongoDB:

It utilizes Mongo's capped collections and tailable cursors to notify subscribers of inserted documents that match a given query.

To use it, a channel must be created and then subscribed to. It can work with MongoDB connection URLs, so it's fairly easy to drop into an existing MongoDB-based Node project. It comes with Mocha/Sinon.JS tests.

Book: An Introduction to libuv

An Introduction to libuv by Nikhil Marathe is a guide to libuv. It covers streams, threads, processes, event loops, and utilities. If you're trying to understand what makes Node different, and how its asynchronous and event-based design works, then this is actually a great guide. Try looking at Basics of libuv: Event loops as an example.


node modules async pubsub

Node Roundup: Node 0.8.6, Axon, NextFlow

Posted on .

You can send in your Node projects for review through our contact form or @dailyjs.

Node 0.8.6

Node 0.8.6 is out, and this release is the first to include binary distributions for all supported Unix systems.

The 0.6 series has also been updated, with the release of 0.6.21. This release fixes a bug in fs.watch that affected Solaris.


Axon (License: MIT, npm: axon) by TJ Holowaychuk is a zeromq-inspired message-oriented socket library. It uses the push/pull and publish/subscribe patterns, and features a lightweight wire protocol that supports binary messages.

Axon is JavaScript, so it might work well in situations where a messaging system is desired but additional software installation is not. The API is friendly for Node developers, particularly the EmitterSocket object which behaves like Node's EventEmitter.

TJ has included information on the protocol and some rough benchmarks.


NextFlow (License: MIT, npm: nextflow) by JP Richardson is a control flow library for Node that has a CoffeeScript-friendly API. Rather than using a chainable API or a list of arguments, NextFlow accepts an object:

next = require('nextflow')

vals = []  
x = 0

next flow =  
  1: ->
  2: ->
    x = Math.random()
  3: (num) ->
  4: ->
  5: ->
    console.log vals[0] #is 1
    console.log vals[1] #is 2
    console.log vals[2] #is x
    console.log vals[3] #is 4

The author has included CoffeeScript comparisons with well-known Node control flow libraries.


frameworks libraries events modules ender pubsub color hyphenation

Ender Roundup: Radio.js, one.color, Hypher

Posted on .

You can send in your Ender-related projects for review through our contact form or @dailyjs. Be sure to also update the Ender package list page on the Ender wiki.


Radio.js (GitHub: uxder/Radio, Licence: MIT, npm / Ender: radio) by Scott Murphy is a simple publish / subscribe library with a well-designed chainable API. Radio.js operates through the $.radio() method from within Ender.

//create topic called changeTabEvent and subscribe myFunction
//publish to the topic changeTabEvent
//unsubscribe myFunction from the topic changeTabEvent
//do all of the above in one line via chaining

Event "channels" are created by the main radio() method.
You can then use the three main API calls to interact with the channels:
subscribe(), broadcast() and
unsubscribe(), each able to take different types and
numbers of arguments.

Radio.js will also work as a stand-alone browser library and in node.js.


one.color (GitHub: One-com/one-color, Licence: BSD, npm / Ender: onecolor) by Peter Müller and One.com is an
amazingly comprehensive color toolkit. It implicitly converts between
RGB, HSV, HSL and CMYK color spaces with or without alpha transparency.
Its API is chainable for composing, adjusting, and serializing color
values. A demo page gives you some
idea of the potential of one.color.


$.color('rgb(102, 76, 230)'). // Can parse CSS color strings
    lightness(+.2, true).     // Implicit conversion to HSL
    red(-.1).                 // Implicit conversion back to RGB
    hex();                    // "#00a6f2"

one.color will also work as a stand-alone browser library and in node.js.


Hypher (GitHub: bramstein/hypher, Licence: BSD, npm / Ender: hypher) by Bram Stein is a small hyphenation engine
compatible with Hyphenation.js
language objects.

Hypher adds soft hyphens to
text strings (Unicode: U+00AD, HTML: &#173; or &shy;) according
to language rules as defined in the patterns objects. Modern browsers
then use these soft hyphens to break words where wrapping is required,
otherwise they are invisible.

Hypher comes with a large number of
language patterns which are also available in the npm repository as
hyphenation.lang (e.g.
hyphenation.en-us or hyphenation.fr). Simply
include hypher and at least one language pattern in your
Ender build.

// generates 'Hy|phen|ation is use|ful when cen|ter jus|ti|fy|ing a text.'
// where `|` is a soft hyphen
$('<p>Hyphenation is useful when center justifying a text.</p>')

Hypher is also available as a jQuery plugin and will work as a stand-alone
browser library and in node.js.

Tidbits and updates

Some minor notable items since the last Roundup:

Bonzo goes 1.0.0

Bonzo, the DOM manipulation library included in The Jeesh, has received a lot of minor fixes this week and its test suite has been
significantly expanded, so much that a bump to 1.0.0 seemed appropriate.

Minor Qwery update

Qwery, the selector engine included in The Jeesh had some minor performance improvements, particularly for IE8,
and can now be configured to turn off use of native
querySelectorAll if required:
$.configure({ useNativeQSA: false }).

Bean does better delegation

Bean, the event manager included in The Jeesh, has received some delegation-love, fixing bugs related to
clone() and erroneous event.currentTarget

Bean is also likely to see a change in the implementation of
on() that will make it (mostly) compatible with the
implementations in Prototype,
jQuery and Zepto. Further details are available on GitHub.

Ender via CDN

After the flurry of recent activity on core Ender modules,
Dustin Diaz has updated the The Jeesh and a more bulky Ender build on S3 this week, so is these builds suit your
needs then point your script tag to one of these CloudFront URLs:

The Jeesh (Qwery, Bonzo, Bean, domReady)


Jeesh++ (Qwery, Bonzo, Bean, domReady, Reqwest, Morpheus, Valentine, Bowser, $script.js)


Twitter's Bootstrap for Ender

Along with the release of Bootstrap
, an Ender compatible version
is now available on npm. Unlike the v1.x port, this new version makes
each plugin available separately so you only need to include the ones
you intend to use in your Ender build.

With all plugins installed, a minimal Ender build for full Bootstrap
functionality comes in at a little under half the size of the equivalent
jQuery Bootstrap.

Further details available on the new
ender-bootstrap repository.