@frmnt/hydra

0.0.2 • Public • Published

Hydra

Hydra is a framework for building networked microservices, supporting both remote procedure call (RPC) and publish-subscribe models of communication.

At a glance

As an example of a the most basic Hydra system, we'll make a single "hello" Service with a sayHello method, and call that method from a separate process using a Client.

A Service has a name and set of named methods:

var hydra = require('@frmnt/hydra');

var hello_service = new hydra.Service('hello', {
    sayHello: function (args, cb) {
        cb(null, 'Hello, ' + args.name + '!');
    }
});

To make requests to a Service you use a Client:

var hydra = require('@frmnt/hydra');

var hello_client = new hydra.Client();

hello_client.remote('hello', 'sayHello', 'world', function (err, response) {
    console.log('Got response: ' + response);
});

Installation

Hydra requires the Node.js ZeroMQ library, which requires ZeroMQ libraries - install those with your system package manager:

$ sudo apt-get install libzmq-dev

Install the Hydra library locally, and the Hydra Server globally:

$ npm install @frmnt/hydra
$ npm install -g @frmnt/hydra-server

Getting started

First make sure the Hydra Server is installed and running:

$ hydra-server
[Hydra Server] Bound to 127.0.0.1:8420

Creating a Service

Create a Service using new hydra.Service(args, methods). The methods argument is an object of named functions; every function is asynchronous and takes a callback as its second argument.

A Service can publish events using service.publish(type, data).

This example creates a Service named "hello" with a single method sayHello(args, cb), and emits an event called hi every 2 seconds:

var hydra = require('@frmnt/hydra');

var hello_service = new hydra.Service('hello', {
    sayHello: function (args, cb) {
        cb(null, 'Hello, ' + args.name + '!');
    }
});

setInterval(function() {
    hello_service.publish('hi', "Just saying hi.");
}, 2000);

Running a Service

When a Service is started it will bind to a random port and register itself with the Hydra Server:

Registered service `hello~9iuma73n` on tcp://127.0.0.1:15544

Creating a Client

Create a Client using new hydra.Client().

Call a remote method of a Service using client.remote(service, method, args, cb). The callback takes two argments, err and response.

Subscribe to events from a Service using client.subscribe(service, type, cb). This callback takes one argument, the incoming event.

This example connects to the "hello" service, calls the sayHello method, and subscribes to its events:

var hydra = require('@frmnt/hydra');

var hello_client = new hydra.Client();

hello_client.remote('hello', 'sayHello', 'world', function (err, response) {
    console.log('Got response: ' + response);
});

hello_client.subscribe('hello', 'hi', function (event) {
    console.log('Got event: ' + event);
});

Running a Client

Assuming the Hydra Server and "hello" service are running, running the client call the "hello" service's remote method sayHello and subscribe to its hi events:

Got response: Hello, world!
Got event: Just saying hi.
Got event: Just saying hi.
Got event: Just saying hi.
^C

Package Sidebar

Install

npm i @frmnt/hydra

Weekly Downloads

5

Version

0.0.2

License

MIT

Unpacked Size

47 kB

Total Files

10

Last publish

Collaborators