Node. js sends streaming data to the client

Source: Internet
Author: User

Node. js sends streaming data to the client
If the data on a requested page is large or a data stream that takes some time to complete, it is better to send the completed data to the client in the form of a stream. In express, the general practice is to send data after the data is completed. For example, when the system command is executed using exec, the callback function is called to process the command output after the command is completed.

  1. Function cmd (command, req, callback ){
  2. Var sys = require ('sys ')
  3. Var exec = require('child_process'cmd.exe c;
  4. Console. log (new Date () + 'run command from '+ req. ip );
  5. Console. log (command );

  6. Exec (command ,{
  7. MaxBuffer: 2000*1024
  8. }, Function (error, stdout, stderr ){
  9. Callback (stdout. trim ());
  10. })
  11. }
The following methods can be used to transmit streaming data.


Continuous write res Mode
The most straightforward method is to continuously write the res object of node, for example:
  1. Var sys = require ('sys '),
  2. Http = require ('http ');
  3. Http. createServer (function (req, res ){
  4. Res. writeHead (200, {'content-type': 'text/html '});
  5. Var currentTime = new Date ();
  6. Sys. puts ('starting sending Time ');
  7. SetInterval (function (){
  8. Res. write (
  9. CurrentTime. getHours ()
  10. + ':' +
  11. CurrentTime. getMinutes ()
  12. + ':' +
  13. CurrentTime. getSeconds () + "\ n"
  14. );

  15. SetTimeout (function (){
  16. Res. end ();
  17. },10000 );

  18. },1000 );
  19. }). Listen (8090, '192. 168.175.128 ');
However, this method has many disadvantages. First, the packaged res objects in the express framework do not support this method. More seriously, it only supports "XHR. readyState = 3 (partial response. Someone also suggested using Sockets. io. If you are interested, you can try it.
  1. WebSocket
  2. WebSocket over Flash (+ XML security policy support)
  3. XHR Polling
  4. XHR Multipart Streaming
  5. Forever Iframe
  6. JSONP Polling (for cross domain)


Use pipe of stream Object
Similar to * nix, which abstracts almost all devices into files, Node abstracts almost all IO operations into Stream operations. Stream is an abstract concept. In short, Stream can take data (in Buffer units) or absorb data.

The following is another example of system command execution, where the stdout stream of spawn is used:
  1. Function performance_stream (command, req, res ){
  2. Console. log (new Date () + 'run command stream from' + req. ip );
  3. Console. log (command );
  4. Var spawn = require ('child _ Process'). spawn;

  5. Var params = command. split (/\ s + /);
  6. Command = params. shift ();
  7. Var cmd_process = spawn (command, params );

  8. Pai_process.stderr.pipe (res );
  9. }
An example of another file stream pipe:
  1. Function file_stream (file, req, res ){
  2. Console. log (new Date () + 'run readfile stream from' + req. ip );
  3. Var fs = require ('fs ');
  4. Var rstream = fs. createReadStream ('/tmp/myfile ');
  5. // Var rstream = fs. createReadStream (file );
  6. Rstream. pipe (res );
  7. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.