jimhigson / oboe.js

A streaming approach to JSON. Oboe.js speeds up web applications by providing parsed objects before the response completes.
http://oboejs.com
Other
4.76k stars 208 forks source link

Oboe Streaming vs. Closing Issue #113

Open ambarc opened 7 years ago

ambarc commented 7 years ago

Xpost from Oboe Google Forum:

I'm trying to use Oboe on some streaming functionality with the following options:

    var options = {
      url: someUrl, 
      method: "POST", 
      cached: false, 
      body: JSON.stringify(someBody), 
    }  

The weird thing I'm seeing with the request is that I only see data coming out of Oboe when I end the stream server side. However, there isn't any data interchange between the server and client when this happens making me think that the data is already client side. Is there some sort of pass through/resolve that I'm missing?

Oboe

oboe(options) // options identical
    .node('result.*', function(message) {
      console.log(data);
    })
    .done(function(things) {
      console.log('we got it', things);
      // we got it
    }).fail(function() {  
      console.log('failing with arguments', arguments);
    });

A couple of weird things happen. Only when I force close the stream server side does the node handler start seeing messages. Also, it gets each key while i'd like to synchronize and get each high level object as it gets sent by the server.

I managed to get it working in Node by doing

request(options) 
    .pipe(JSONStream.parse('*')) 
    .pipe(es.map(function(message:Message) { 
      handle(message) 
    }));

Each time a JSON object comes up message is that entire object that the server sends one at a time.

nhducit commented 7 years ago

What did you do in handle(message) function. Check out this example: https://github.com/jimhigson/oboe.js/blob/master/benchmarking/benchmarkServer.js.

When you want to streaming data from node server to client. Use res.write

  if( i % 2 == 0 ) {

         res.write(JSON.stringify({
            "id": i,
            "url": "http://localhost:4444/item/" + i         
         }));
      } else {
         res.write(JSON.stringify({
            "id": i         
         }));      
      }