Skip to content

Instantly share code, notes, and snippets.

@bastianwegge
Created May 6, 2016 16:16
Show Gist options
  • Save bastianwegge/f2ab7c4d68ab032579e69e69346b3203 to your computer and use it in GitHub Desktop.
Save bastianwegge/f2ab7c4d68ab032579e69e69346b3203 to your computer and use it in GitHub Desktop.
// client (client.js)
require('seneca')()
// a local pattern
.add('say:hello', function (msg, respond){ respond(null, {text: "Hi!"}); })
// send any role:math patterns out over the network
// IMPORTANT: must match listening service
.client({ type: 'tcp', pin: 'role:math' })
// executed remotely
.act('role:math,cmd:sum,left:1,right:2',console.log)
// executed locally
.act('say:hello',console.log);
// plugin (math.js)
module.exports = function math(options) {
this.add('role:math,cmd:sum', function sum(msg, respond) {
respond(null, { answer: msg.left + msg.right });
});
this.add('role:math,cmd:product', function product(msg, respond) {
respond(null, { answer: msg.left * msg.right });
});
this.wrap('role:math', function (msg, respond) {
msg.left = Number(msg.left).valueOf();
msg.right = Number(msg.right).valueOf();
this.prior(msg, respond);
});
};
// service (service.js)
require('seneca')()
.use('math')
// listen for role:math messages
// IMPORTANT: must match client
.listen({ type: 'tcp', pin: 'role:math' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment