block by joyrexus 8675868

Multi-async responses w/ actorify

Another quick demo of the actorify module, which turns any duplex stream into an actor.

We show a client sending a single request that gets multiple async responses from the server. In particular, we’re simulating sending an image over the wire for resizing. The client specifies the thumbnail sizes it would like sent back. The server handles each request with multiple async responses, one per thumbnail size being requested.

The actor/RPC comparison below was taken straight from the README.


Actors provide a simple primitive similar to RPC, but as a small unit — the “actor”. Traditional RPC can be easily implemented on top of actors, however the isolated actor can be very useful for maintaining state between a conversation with another actor.

Actors shine in cases where the requester expects multiple responses. For example suppose you send a request to produce N thumbnails for an image, with traditional RPC you might choose to wait until they’re all completed and then respond with the URLs — with actors you can simply send messages as the thumbnails are produced.

The requesting end of this use-case might look something like this:

var sizes = ['150x150', '300x300'];
actor.send('get thumbnails', image, sizes)

actor.on('thumb', function(size, thumbnail){
  console.log('%s thumbnail received', size);
});

Followed by the receiving end producing the thumbnails:

actor.on('get thumbnails', function(image, sizes){
  sizes.forEach(function(size){
    resize(image, size, function(buffer){
      actor.send('thumb', size, buffer);
    });
  });
});

index.js

index.coffee

package.json