A simple Somata service with Node.js
A Somata service exposes methods that can be called by a Somata client elsewhere in your code. Let's make a simple SpeakerService that exposes two methods to say hello and goodbye.
somata = require 'somata'
# Create a new Somata service named 'hello' with a single method 'sayHello'
service = new somata.Service 'hello', {
sayHello: (name, cb) ->
cb null, 'Hello, ' + name + '!'
}
var somata = require('somata');
var service = new somata.Service('hello', {
sayHello: function(name, cb) {
return cb(null, 'Hello ' + name + '!');
}
});
somata = require 'somata'
client = new somata.Client
client.remote 'hello', 'sayHello', 'Fred', (err, response) ->
console.log 'Response: ' + response
var somata = require('somata');
var client = new somata.Client;
client.remote('hello', 'sayHello', 'Fred', function(err, response) {
return console.log('Response: ' + response);
});
somata = require 'somata'
# Create a new Somata service named 'speaker'
service = new somata.Service 'speaker', {
# With a few methods
sayHello: (name, cb) ->
cb null, 'Hello, ' + name + '!'
sayGoodbye: (name, cb) ->
cb null, 'Goodbye, ' + name + '!'
}
var service, somata;
somata = require('somata');
service = new somata.Service('speaker', {
sayHello: function(name, cb) {
return cb(null, 'Hello, ' + name + '!');
},
sayGoodbye: function(name, cb) {
return cb(null, 'Goodbye, ' + name + '!');
}
});
Now let's call these functions in order from another piece of code.
somata = require 'somata'
# Create a new Somata client
client = new somata.Client
# Execute SpeakerService.sayHello('world') and get the result...
client.remote 'speaker', 'sayHello', 'world', (err, hello) ->
console.log '[speaker.sayHello] response: ' + hello
# ... then execute SpeakerService.sayGoodbye('world')
client.remote 'speaker', 'sayGoodbye', 'world', (err, goodbye) ->
console.log '[speaker.sayGoodbye] response: ' + goodbye
process.exit()
var client, somata;
somata = require('somata');
client = new somata.Client;
client.remote('speaker', 'sayHello', 'world', function(err, hello) {
console.log('[speaker.sayHello] response: ' + hello);
return client.remote('speaker', 'sayGoodbye', 'world', function(err, goodbye) {
console.log('[speaker.sayGoodbye] response: ' + goodbye);
return process.exit();
});
});
Start the service, then run the client:
$ coffee speaker-service.coffee & Somata service listening on localhost:15555... $ coffee speaker-client.coffee Found service speaker@localhost:15555 [speaker.sayHello] response: Hello, world! [speaker.sayGoodbye] response: Goodbye, world!
$ node speaker-service.js & Somata service listening on localhost:15555... $ node speaker-client.js Found service speaker@localhost:15555 [speaker.sayHello] response: Hello, world! [speaker.sayGoodbye] response: Goodbye, world!
Subscribe to a remote event
Each Somata service can also publish events. Any client in the 'cluster/system' can then subscribe to these events as you would with any javascript event. Let's create a Service that will publish an 'announcement' with a message every second and print out the message elsewhere by subscribing to the 'announcement' event.
somata = require 'somata'
# Create a service with no methods
service = new somata.Service 'publisher', {}
# Publish an 'announcement' event with message 'Hey there, world!'
sendAnnouncement = ->
service.publish 'announcement', 'Hey there, world!'
# Publish every second
setInterval sendAnnouncement, 1000
var sendAnnouncement, service, somata;
somata = require('somata');
service = new somata.Service('publisher', {});
sendAnnouncement = function() {
return service.publish('announcement', 'Hey there, world!');
};
setInterval(sendAnnouncement, 1000);
Create a Client that subscribes to the 'announcement' event and logs the message
somata = require 'somata'
client = new somata.Client
# Subscribe to an 'announcement' event from the 'publisher' service
client.on 'publisher', 'announcement', (message) ->
console.log '[subscriber.onAnnouncement] New message:', message
var client, somata;
somata = require('somata');
client = new somata.Client;
client.on('publisher', 'announcement', function(message) {
return console.log('[subscriber.onAnnouncement] New message:', message);
});
Start the service and client
$ coffee publisher-service.coffee & [didBind] Socket mulj67id bound to tcp://0.0.0.0:36206... Registered service `publisher~ho5xqalh` on tcp://0.0.0.0:36206 $ coffee subscriber-client.coffee [Client.subscribe] publisher~u3sg0zoj : announcement Subscribing <mic9pdgi::publisher:announcementy7wq> [subscriber.onAnnouncement] New message: Hey there, world! [subscriber.onAnnouncement] New message: Hey there, world! [subscriber.onAnnouncement] New message: Hey there, world! ...
$ node publisher-service.js & [didBind] Socket mulj67id bound to tcp://0.0.0.0:36206... Registered service `publisher~ho5xqalh` on tcp://0.0.0.0:36206 $ node subscriber-client.js [Client.subscribe] publisher~u3sg0zoj : announcement Subscribing <mic9pdgi::publisher:announcementy7wq> [subscriber.onAnnouncement] New message: Hey there, world! [subscriber.onAnnouncement] New message: Hey there, world! [subscriber.onAnnouncement] New message: Hey there, world! ...
You can even subscribe to Services' events from the browser with somata-socketio.
For more examples, see https://github.com/somata/somata-node/tree/master/examples