Develop services as single processes, deploy them as multiple.
Develop services as single processes and deploy them as multiple.
Let's write a webserver that stores and receives data from a leveldb, and operates on querystrings. Since we want to be able to split up the http server and the leveldb later, we give each a role:
var role = require'role';var level = require'level';var multilevel = require'multilevel';var http = require'http';var qs = require'querystring';// we define the `db` roleroleset'db'var db = level__dirname + '/db';returnreturn multilevelserverdb;;// we define the `main` roleroleset'main'var db = multilevelclient;// we request the `db` roleroleget'db'spipedbcreateRpcStreampipes;;httpcreateServervar query = qsparserequrlsplit'?'slice1join'';if querygetdbgetquerygetif err resend'not found';else resendval;;else if queryputdbputqueryput queryvalueif err resendStringerr;else resend'ok';;elseresend'try\n\n/?put=foo&value=bar\n\n/?get=foo';listen8000console.log'~> localhost:8000';;;
main is a special role which will always be executed immediately. All the
other rules will be executed when requested via
Now, to start as a single process:
$ node example/simple.js$ open
To start as two seperate processes:
$ HUB=7888 ROLE=main node example/simple.js$ CLIENT=7888 ROLE=db node example/simple.js
To start as three seperate processes (mind synchronisation):
$ HUB=7888 ROLE=main node example/simple.js$ CLIENT=7888 ROLE=db node example/simple.js$ CLIENT=7888 ROLE=db node example/simple.js$ # Now you can just kill and recreate one database at a time and the sitewill stay up.
When using distributed/production mode you just have to make sure that there's always have at least one hub running.
fn with a stream for role
name. When the connection is lost and a new
one is made or already available, call
fn to be role
fn should return a Stream or a Function
that returns a Stream.
Pass none for local mode. Pass
HUB: Be a hub and listen on that port.
CLIENT: Be a client and listen on that port.
ROLE: The role to serve. Multiple roles are activated when you pass a comma-seperated list, like
With npm do
$ npm install role
Copyright (c) 2013 Julian Gruber <email@example.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.