0

I'm trying to follow the tutorial here:

http://nodetuts.com/tutorials/2-webtail-nodejs-child-processes-and-http-chunked-encoding.html#video

The following code works

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

http.createServer(
function (request, response) {
    response.writeHead(200, {
        'Content-Type':'text/plain'
    });
    var tail_child = spawn('tail', ['-f', 'temp.txt']);

    request.connection.on('end', function () {
        tail_child.kill();
    });

    tail_child.stdout.on('data', function (data) {
        console.log(data.toString());
        response.end(data.toString());

    });

}).listen(9000);

However the browser does not receive updates to temp.txt. If I replace

response.end(data.toString());

with

response.write(data.toString());

It appears to block and nothing is rendered in the browser.

Edit: I would like the browser to continuously display any appends to the text file in real time as per the tutorial

4
  • I have made an edit to clarify Commented Jan 25, 2012 at 23:49
  • Yes, because if you never call response.end() the response is never sent. Commented Jan 25, 2012 at 23:51
  • In the tutorial there is no response.end() yet new appends appear to 'stream' into the browser Commented Jan 26, 2012 at 0:06
  • Welll, perhaps I have missed something; I'm by no means a node expert. I have played around with it and once forgot to call end(). The response was never sent. Commented Jan 26, 2012 at 0:08

1 Answer 1

2

You should try using curl instead to test out transfer-encoding: chunked. A lot of web browsers won't render the content in chunks, probably because it isn't really efficient to render HTML in that fashion. If you put enough data into the response eventually the browser may start rendering it as you'd expect though, but it isn't a solution I'd use normally.

If you want to stream data to a web browser in the way you're hoping, I'd use websockets or possibly an ajax call made to your streaming page that would render html as it comes, since an ajax call should fire events for each chunk.

Sign up to request clarification or add additional context in comments.

1 Comment

I've tested this out, and the code in the original question works fine with response.write() if you include the 'Transfer-Encoding': 'chunked' header and push enough data in. Try adding much more data temp.txt using less temp.txt >> temp.txt. I also added 'Content-Type':'text/plain; charset=UTF-8', but not sure whether that made a difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.