Note that there are some explanatory texts on larger screens.

plurals
  1. POStreaming data with Node.js
    text
    copied!<p>I want to know if it is possible to stream data from the server to the client with Node.js. I want to post a single AJAX request to Node.js, then leave the connection open and continuously stream data to the client. The client will receive this stream and update the page continuously.</p> <h2>Update:</h2> <p>As an update to <a href="https://stackoverflow.com/questions/2558606/stream-data-with-node-js/2563459#2563459">this answer</a> - I cannot get this to work. The <code>response.write</code> is not sent before you call <code>close</code>. I have set up an example program that I use to achieve this:</p> <p>Node.js:</p> <pre><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); </code></pre> <p>HTML: </p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Testnode&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;!-- This fields needs to be updated --&gt; Server time: &lt;span id="time"&gt;&amp;nbsp;&lt;/span&gt; &lt;!-- import jQuery from google --&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt; &lt;!-- import jQuery --&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ // I call here node.localhost nginx ports this to port 8000 $('#time').load('http://node.localhost'); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Using this method I don't get anything back until I call <code>close()</code>. Is this possible or should I go with a long poll approach instead where I call the load function again as one comes in?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload