user780756
user780756

Reputation: 1484

Write stdout to a div as program executes with Node-Webkit?

In my Node-Webkit app, I use exec() to execute a child application. The application prints out some debug information to the shell.

The problem is stdout only sends the text after the child application has exited.

Is there a way to update a div element with the stdout info as the child application is running?

var exec = require('child_process').exec;

exec(executableFile,  function (error, stdout, stderr) {

    if (stdout) {
        // this only updates after the app exits
        console.log('stdout: ' + stdout);
        $('#console-output').append('stdout: ' + stdout +'<br/>');
    }

    if (error !== null) {
      console.log('exec error: ' + error);
    }

});

Upvotes: 0

Views: 300

Answers (1)

Ben Fortune
Ben Fortune

Reputation: 32127

You need to use spawn instead of exec.

Spawn sends the data back as a stream, so you can tap into it with stdout.on(fn).

var spawn = require('child_process').spawn;

var proc = spawn(executableFile);

proc.stderr.on('data', function(stderr) {
    console.log('exec error: ' + stderr); 
});

proc.stdout.on('data', function(stdout) {
    $('#console-output').append('stdout: ' + stdout.toString() + '<br/>');
});

Upvotes: 1

Related Questions