c3io exploits stdin and stdout for inter-process communication in order to communicate with other processes.
If you only want to communicate with parent to child in nodejs you simply can use standard child_process communication.
WORK IN PROGRESS - NOT STABLE
Have a look at c3docker using c3io as means of communicating with docker container.
var c3io = require('c3io')(options),
spawn = require('child_process').spawn
c3io.on('message', function(msg) {
// input request
if ( msg.stdin ) {
// send message [string]
this.stdin = 'Hello World'
}
// stderr stream
else if ( msg.stderr ) {
console.log('stderr', msg.toString('utf8'))
}
// stdout stream
else {
console.log('stdout', msg.toString('utf8'))
}
})
var child = spawn(
'casperjs',
['test.js'])
c3io.pipe(child)
First a new c3io instance is created, which is basically an EventEmitter.
Then you can listen to message
as you would with child processes.
Your child process can invoke this with simple commands. Here e.g. for casperjs.
var system = require('system'),
casper = require("casper").create();
system.stdout.write('Hello, system.stdout.write!\n');
system.stdout.writeLine("c3io!req");
var line = system.stdin.readLine();
system.stdout.writeLine(line);
casper.echo('c3io!stp');
casper.exit();
notice [ default ] for c3io is each line a command to be passed on. For that make sure it
ends with an EOL. You can change this behavior with options.protocol
.
As you maybe noticed there is also the ability to push for certain commands.
First there is the initial c3io!
telling c3io to look for the given command.
You can change the initial sequence within the environment.
env c3ctr="foo" c3len=1 node example.js
c3len
configures how long the command name is. [default: 3]
Currently there are following sequences. (for usage see latter example)
- c3io!req
triggers an message event in order to replay to the input request
c3io!stp is c3docker specific.
- protocol [ newline | native ] ~
The same format is used to send messages in both directions.
newline
each line is a command to be passed on. For that make sure it ends with an EOL.
native
each message is serialized using Buffer and is preceded with 32-bit message length in native byte order.
It is possible to define your own commands.
var c3io = require('c3io')
c3io.options.r2d2.wrt = function(_data) {
require('fs').writeFile('example.txt', _data)
// skip message (no event will be emitted)
return null
}
And executing the command in child process.
var system = require('system'),
file_c = "Write something to File!"
// if you have line breaks within string make sure to handle them. e.g. through serializing.
system.stdout.writeLine("c3io!wrt" + file_c);
Please refer to test/main.js
npm install c3io --save
or directly from github
npm install fentas/c3io