
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
Applications rely on different services like databases or message queues. Before your application can do any work you need to connect to those services. Runsv will help you with thatđź’Ş.
const runsv = require('runsv').create();
// Require your defined services
const postgresql = require('runsv-pg')();
const redis = require('runsv-redis')();
const app = require('./my-web-app-service');
// your web app requires pg and redis
runsv.addService(app, pg, redis);
// start order: redis → pg → app
runsv.start(function (err, clients) {
// pg, redis and app are ready and running
const {pg, redis} = clients;
// do your magic here...
});
process.once('SIGTERM', function(){
// stop order: app → pg → redis
runsv.stop();
});
runsv.addService(app, pg, redis) adds services app, pg and redis. It also defines that app requires pg and redis.
runsv will create a dependency graph and start/stop them in the correct order.
// complex service scenario
runsv.addService(a, b, c); // a needs b and c
runsv.addService(z, a); // z needs a
runsv.addService(x, c); // x needs c
// start order: b → x → c → a → z
// stop order: z → a → c → x → b
Services that rely on another services will have access to their clients at start time. See services definition below.
A service is just an object with the following interface:
name Service name. A stringstart (dependencies, callback) A function that starts the service
callback(err) when service is ready. Pass an error if something went wrongstop (callback) A function that stops the service
callback(err) when service has stopped. Pass an error if something went wronggetClient() A function that returns the client. I.e a database clientconst redis = require('redis');
function createRedisService(redisConf) {
let client;
// #name will be used by other services/code to access this service client
const name = 'redis';
function start(callback) {
client = redis.createClient(redisConf);
// callback once the client is ready
client.once('ready', function(err){
return callback(err);
});
}
function stop(callback) {
if(!client){
// nothing to do here
return callback();
}
client.quit(function (err) {
client = null;
return callback(err);
});
}
function getClient() {
return client;
}
return {
name,
start,
stop,
getClient
};
}
// a service that relies on the redis service
function createComplexService() {
let client;
const name = 'complex';
function start(dependencies, callback) {
const redisClient = dependencies.redis;
client = createClient(redisClient);
}
function stop(callback) {
client = null;
return callback();
}
function getClient() {
return client;
}
return {
name,
start,
stop,
getClient
};
}
//...
let redis = createRedisService();
let complex = createComplexService();
sv.addService(complex, redis); // complex relies on redis
sv.start(/*...*/);
Services can be defined with any API style: callback, promises or async/await.
RunSV can be consumed with callbacks, promises or async/await as well.
To create this interface just call async() on the runsv object.
const runsv = require('runsv').create().async();
// ...
await runsv.start();
// or, with promises
runsv.start()
.then(clients => /*...*/)
.catch(error => /*...*/);
You can define services with callback, promises or async/await interface styles.
// async example
const myService = {
name: 'myService',
async start(deps){ /**/}
async stop(){ /**/}
}
ℹ️ You can mix services with different interfaces
getService(name) Get a service by its nameaddService(service, [...dependencies]) Adds a service with optional dependencieslistServices() Gets a list of services i.e ['pg', 'redis']getClients(...only) Get a bunch of clients, If no client is specified it returns all clientsstart(callback) Start all servicesstop(callback) Stop all servicesasync() returns an async/await interface// Events example
runsv.addService(app, pg, redis); // app requires pg and redis
// Log service start. I.e print "pg ready. Took 10ms to become ready"
runsv.on('start', (service, res) => console.log(service, 'ready.', 'Took', res.took+'ms to become ready'));
// Log services that are not starting/stopping. I. waiting for pg to start (200ms)
runsv.on('waiting', (service, event, ms) => console.log(`waiting for ${service} to ${event} (${ms}ms)`));
// Log service stop
runsv.on('stop', (service, res) => console.log(service, 'stopped', 'took', res.took+'ms to stop'));
runsv.start(/*...*/);
A shared context is passed to every hook.
You can augment that context to share information between hooks.
Shared context is not shared with services.
Hooks can use callback, promise or async/await API.
Do not pass promisified functions as runsv might not be able to properly handle them.
addService(service, [,deps]) returns an array with two hooks for the added service.
const [setupPG, teardownPG] = runsv.addService(pg);
runsv.addService(app, pg, redis); // app requires pg and redis
setupPG(function(ctxt, deps, callback){ // could also be setupPG(async function(ctxt, deps))
// ctxt is a shared context between all hooks
ctxt.database = 'my-db-' + Date.now(); // create a random db
deps.pg.query(`create database ${ctxt.database};`, callback);
});
teardownPG(function(ctxt, deps, callback){
deps.pg.query(`drop database ${ctxt.database};`, callback);
});
Some wrappers for popular node modules:
nanopg service wrapperredis service wrapperFAQs
Define and orchestrate your application services
We found that runsv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.