views:

1103

answers:

1

I want to know if it is possible to stream data from the server to the client with Node.js. From what I can understand all over the internet is that this has to be possible, yet I fail to find a correct example or solution.

What I want is a single http post to node.js with AJAX. Than leave the connection open and continuously stream data to the client.

The client will receive this stream and update the page continuously.

Update 1

As an update to this answer: http://stackoverflow.com/questions/2558606/stream-data-with-node-js/2563459#2563459

I tried this already and I tried it again after your anser. I can not get this to work. The response.write is not send before you call close. I have set up an example program that i use to achieve this.

Node.js code:

var sys = require('sys'), 
http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    var currentTime = new Date();
    setInterval(function(){
        res.write(
            currentTime.getHours()
            + ':' + 
            currentTime.getMinutes()
            + ':' +
            currentTime.getSeconds()
        );
    },1000);
}).listen(8000);

Here the html code:

<html>
<head>
<title>Testnode</title>
</head>
<body>

<!-- This fields needs to be updated -->
Server time: <span id="time">&nbsp;</span>

<!-- import jQuery from google -->
<script 
  type="text/javascript"  
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;
</script>

<!-- import jQuery -->
<script type="text/javascript">
$(document).ready(function(){
    // I call here node.localhost nginx ports this to port 8000
    $('#time').load('http://node.localhost');
});
</script>
</body>
</html>

Using this method I don't get anything back until I call close(). is this possible or should I rather go with a long poll approach where I call the load function again as one comes in.

i really rather want to stream this. I hope someone can help.

+2  A: 

It is possible. Just use response.write() multiple times.

var body = ["hello world", "early morning", "richard stallman", "chunky bacon"];
// send headers
response.writeHead(200, {
  "Content-Type": "text/plain"
});

// send data in chunks
for (piece in body) {
    response.write(body[piece], "ascii");
}

// close connection
response.close();

You may have to close and reopen connection every 30 seconds or so.

EDIT: this is the code I actually tested:

var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    var currentTime = new Date();
    sys.puts('Starting sending time');
    setInterval(function(){
        res.write(
            currentTime.getHours()
            + ':' +
            currentTime.getMinutes()
            + ':' +
            currentTime.getSeconds() + "\n"
        );

        setTimeout(function() {
            res.close();
        }, 10000);

    },1000);
}).listen(8090, '192.168.175.128');

I connected to it by Telnet and its indeed gives out chunked response. But to use it in AJAX browser has to support XHR.readyState = 3 (partial response). Not all browsers support this, as far as I know. So you better use long polling (or Websockets for Chrome/Firefox).

EDIT2: Also, if you use nginx as reverse proxy to Node, it sometimes wants to gather all chunks and send it to user at once. You need to tweak it.

Kuroki Kaze
I am testing this approach, but I am having trouble accessing the node.js script trough Ajax on my page. I think this has something to do with some cross domain thing. I am going to work on this later on and see if this works as expected.
Saif Bechan
I updated the question I hope you can help me with this.
Saif Bechan
`jquery.load` waits for all body to arrive before firing callback. You need to use something different. See http://api.jquery.com/load/
Kuroki Kaze
Do you have any idea on what kind of difference?
Saif Bechan
Some low-level stuff, I think. `XMLHTTPRequest` maybe. Anything that can deal with chunked responses.
Kuroki Kaze
Are you sure this should work. Or are you just thinking theoretically it should work. Because I am trying to get it chunked but its just not displaying. The Transfer-Encoding is chunked but Node.js only sends it when you call close. Else i should just use a long poll solution.
Saif Bechan
I'll check it on my node machine (0.1.31)
Kuroki Kaze
Yes, maybe longpoll will be better.
Kuroki Kaze