node. js sends streaming data to the client

Source: Internet
Author: User

If a requested page data is large, or a data flow that takes a certain amount of time to complete, it is a good practice to send the completed data to the client in a streaming manner. In Express, the general practice is that after the data is finished, the callback function is called to process the command output after the command is executed, such as when the exec executes the system command.
    1. function cmd (command,req,callback) {
    2. var sys = require (' sys ')
    3. var exec = require (' child_process '). exec;
    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 implement streaming data transfer.


Continuous Write res mode
The most straightforward way is to keep writing the Res object for node, for example:
  1. var sys = require (' sys '),
  2. HTTP = require (' http ');
  3. Http.createserver (function (req, res) {
  4. Res.writehead ($, {' 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, there are many drawbacks to this approach, first of all the RES objects packaged in the Express framework do not support this way of use, and more seriously, only browsers that support "Xhr.readystate = 3 (partial response)" Can do so. Other people suggest using Sockets.io, interested friends can try.
    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)


Pipe using the Stream object
Like *nix, which abstracts almost all devices into files, Node abstracts almost all IO operations into the operation of Stream. Stream is an abstract concept, in short it takes data (in Buffer), or something that absorbs data.

Here is another example of the system command execution above, which uses the spawn stdout stream:
    1. function Cmd_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. Cmd_process.stderr.pipe (RES);
    9. }
Another example of file flow 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. }

node. js sends streaming data to the client

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.