Skip to content

Instantly share code, notes, and snippets.

@spion
Created August 23, 2012 15:30
Show Gist options
  • Save spion/3437767 to your computer and use it in GitHub Desktop.
Save spion/3437767 to your computer and use it in GitHub Desktop.
Complete rpc interface for node.
// on the command line:
// ./processPool --port poolport
// on any machine
// ./processWorker --connect poolip:poolport
// The process pool is a registry that delegates services to
// workers and knows which services are registered where.
var pool = ProcessPool.connect(9000);
// The client can get a service by name.
// Getting waits until the service becomes
// available. Afterwards the client...
pool.get('svc-name', function(service) {
// can send messages to it,
service.send('msgtype', data, function(err, reply) { ... });
// can subscribe to events,
service.on('event', function(data, reply) {
// and reply to them if needed
reply(stuff);
});
// can access and pipe stuff to/from its streams
service.stream('streamname').pipe(localstream);
});
// Clients can ask for a service name only if its active at the moment
// Otherwise service will be null
pool.query('svc-name', function(service) {
if (!service) pool.create('svc-name', ...)
});
// Clients can also create new serviceblockss by name.
// They will be created on a random worker from
// the worker pool
pool.run('svc-name', function(service) {
// the function initializes a service which...
stuff = require('stuff');
// can listen to events sent by the client and reply to them,
// if needed. Also, the client is accessible when message is received.
service.on('msgtype', function(data, reply, client) { reply(response); });
setInterval(function() {
// can send events to the clients and optionally get
// replies
var arr = [];
service.send('event', eventData, function(reply, client) {
arr.push(stuff.process({reply:reply, client:client}));
});
// or just to a single client
service.clients[0].send('event', eventData, function(reply) { });
});
// can pipe its streams to virtual service streams.
localstream.pipe(service.stream('streamname'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment