Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

HTTP Body Streaming with Javascript

HTTP Body Streaming with Javascript

Problem

I am writing a debug/admin node server that allows users to execute a long-running process on the machine. I want to stream the output of the child process to the form they began the action from.

I can do this with sockets, but I have to have the client subscribe to a channel, and I have to post messages to the whole channel when they only have to do with the one client.

I'd prefer to be able to stream the Http Body down to the client. I can do this fairly easily with node: just keep writing to the request's socket, call end when I'm done.

Is there any way to use XhrHttpRequest to call a web service, have it fire events whenever new data is available, and a final event when it closes? Possible with jQuery?

Note that this isn't really the same use case as normal real-time updates, for which sockets are a good choice. This is a single request. I just want to get the response in pieces.

Problem courtesy of: Sean Clark Hess

Solution

What I was hoping isn't possible: you can't make an xhr http request and keep it open, parsing chunks at a time.

Here is a summary of people's suggestions

  1. Use socket.io anyway, and change your architecture to support pushing events.
  2. Use socket.io, but make requests through it, as if you were hitting urls. Make a little url router on the server side of socket.io and stream stuff down all you want.
  3. Keep the initial html page open and parse it as you go (not feasible for my implementation)
  4. (3), but in a hidden iframe.

I went with 2.

Solution courtesy of: Sean Clark Hess

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

HTTP Body Streaming with Javascript

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×