0.4.1 • Published 8 years ago

run-or-attach v0.4.1

Weekly downloads
12
License
MIT
Repository
github
Last release
8 years ago

run-or-attach

kickstart Node.js daemon on first demand and attach to it

Split your application or utility into cli-part and daemon-part. Then connect to daemon in cli-part. Daemon will be started on first demand.

/* main file */
var attach = require('run-or-attach')

attach('/tmp/sock', require.resolve('./path-to-worker'))
.then(function (flow)
{
	/* flow is a function for pushing JSON to daemon */
	flow({ x: Math.random() })

	/* flow.recv is for receiving JSON answers from daemon */
	flow.recv = function (r)
	{
		console.dir(r)
	}

	flow.request({ x: 2 })
	.then(function (r)
	{
		console.info('request:')
		console.dir(r)
	})
})
/* worker file */
var Worker = require('run-or-attach/worker')

var worker = Worker()

/* recv is for handling incoming messages/requests */
worker.recv = function (data)
{
	data.x += 1

	// answers can be sync or async
	// return falsy value to answer nothing
	return new Promise((rs) => { setTimeout(() => rs(data), 100) })
}

worker.conn = function (flow)
{
	// push realtime to client without waiting for request
	var next = 0
	setInterval(() => { next = next + 1; flow({ realtime: next }) }, 1000)
}
0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago