Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created January 28, 2014 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/8675868 to your computer and use it in GitHub Desktop.
Save joyrexus/8675868 to your computer and use it in GitHub Desktop.
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);
    });
  });
});
net = require 'net'
actorify = require 'actorify'
# server
connect = (sock) ->
actor = actorify sock
sendThumbs = (image, sizes) ->
msg = 'creating thumbnails of %s byte image -> %s'
console.log(msg, image.length, sizes.join(', '))
sizes.forEach (size) ->
thumbnail = new Buffer('thumb data')
actor.send('thumb', size, thumbnail)
actor.on 'get thumbnails', sendThumbs
net.createServer(connect).listen(3000)
# client
getThumbs = ->
sock = net.connect(3000)
actor = actorify(sock)
image = new Buffer('faux image')
sizes = ['150x150', '300x300']
actor.send('get thumbnails', image, sizes)
log = (size, thumbnail) ->
console.log('%s thumbnail received', size)
actor.on('thumb', log)
setInterval(getThumbs, 500)
var net = require('net');
var actorify = require('actorify');
// server
var connect = function(sock) {
var actor, sendThumbs;
actor = actorify(sock);
sendThumbs = function(image, sizes) {
var msg;
msg = 'creating thumbnails of %s byte image -> %s';
console.log(msg, image.length, sizes.join(', '));
sizes.forEach(function(size) {
var thumbnail;
thumbnail = new Buffer('thumb data');
actor.send('thumb', size, thumbnail);
});
};
actor.on('get thumbnails', sendThumbs);
};
net.createServer(connect).listen(3000);
// client
var getThumbs = function() {
var actor, image, log, sizes, sock;
sock = net.connect(3000);
actor = actorify(sock);
image = new Buffer('faux image');
sizes = ['150x150', '300x300'];
actor.send('get thumbnails', image, sizes);
log = function(size, thumbnail) {
console.log('%s thumbnail received', size);
};
actor.on('thumb', log);
};
setInterval(getThumbs, 500);
{
"name": "actorify-multi-async-demo",
"version": "0.0.1",
"description": "Demo of how to use actorify for multi-async responses.",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"actorify": "~0.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment