strongloop / loopback

LoopBack makes it easy to build modern applications that require complex integrations.
http://loopback.io
Other
13.22k stars 1.2k forks source link

Event streams clarification #1653

Closed ktmn closed 9 years ago

ktmn commented 9 years ago

When I use code from this page: https://docs.strongloop.com/display/public/LB/Realtime+server-sent+events

var MyModel = app.models.MyModel;
MyModel.createChangeStream(function(err, changes) {
    changes.pipe(es.stringify()).pipe(process.stdout);
});

The above code writes to process.stdout, as expected, but when I listen to http://localhost:3000/api/MyModels/change-stream?_format=event-stream nothing appears there. I do get the successful open event, but no 'data' event when I create something in my model.

Here's what I type into chrome dev console to listen:

var urlToChangeStream = 'http://localhost:3000/api/MyModels/change-stream&_format=event-stream';
var src = new EventSource(urlToChangeStream);
src.addEventListener('data', function(msg) {
  // Never get this event, even though the changes are written to process.stdout by the above code
  var data = JSON.parse(msg.data);
  console.log('save', data); // the change object
});
src.addEventListener('open', function(msg) {
  // This works if I use the above url
  console.log('Open', msg);
});
src.addEventListener('error', function(msg) {
  // This works if I use an invalid url
  console.log('Error', msg);
});

What do I need to do with the 'changes' to get them to appear at the /api/.../change-stream url? Should it work by default? What does pipe(es.stringify()) do?

Only thing that randomly pops up in chrome console is

GET http://localhost:3000/api/MyModels/change-stream net::ERR_INCOMPLETE_CHUNKED_ENCODING

Also what is the "_format=event-stream"? Is it necessary, are there other formats? Are there more docs about change-stream, cause I can't seem to find anything other than the link at the start of this post.

superkhau commented 9 years ago

@crandmck ^ I assumed you'll need to get more info from @ritch

crandmck commented 9 years ago

@ktmn Thanks for the report. Have you looked at https://github.com/strongloop/angular-live-set-example? That's about all we have for change streams other than the doc you referenced...

Also, have a look at https://github.com/crandmck/loopback-realtime. I created this as a super-simple example of server-sent events. I have seen the ERR_INCOMPLETE_CHUNKED_ENCODING​ error, but am not seeing it now. Sorry I can't help you more.

I don't know the answers to your questions, but will try to get @ritch to weigh in. He's the expert.

crandmck commented 9 years ago

@ktm I forgot to say: Be sure your model has at least one property, and be sure that you've created a model instance (i.e. a record).

ktmn commented 9 years ago

@crandmck Thanks for the loopback-realtime example. The code was quite identical to mine, yet mine didn't work and the example did. Ended up copying over almost everything from the example to my project, the model, the datasource, even the index.html, still no cigar.

Then I found a difference between my project and the example. A line in my server/middleware.json:

    "compression": {},

I don't know where it came from, I don't remember if I've put it there for a reason, but once I removed it I started getting the events from the stream. So there's that thing solved.

mikemaccana commented 9 years ago

Greetings from the future! SSE can work with compression:

    res.write('data: ' + JSON.stringify(data) + '\n\n');
    // Needed to work with npm 'compression' enabled
    res.flush()
EmileSpecs commented 8 years ago

issue I have the same issue, even with the https://github.com/strongloop/angular-live-set-example example.

I don't have compression in my middleware stack, with compression it doesn't work at all. So the events work just fine, but the errors are annoying, because to filter through it all looking for other issues is problematic. Also it just doesn't look great when you have 100's of errors in the console after a couple of hours...

The error writes to console every 2 minutes it seems. So it would seem like at some point the EventSource connection to the loopback server times out and then automatically reconnects (since it still works even with the errors).

So in loopback, is there a way to send some "keep alive" packets to the client every minute or so?

If no one else is experiencing this issue, what causes it on my PC?

Any help would be appreciated!

superkhau commented 8 years ago

@EmileSpecs I suggest creating your own new issue so that someone may triage your issue. This issue already closed, so you won't get much attention here.

ghost commented 8 years ago

EmileSpecs , I have the same issue and didn't find any solution or help.. https://github.com/strongloop/angular-live-set/issues/14

it is closed without any solution,,

KishoreBarik commented 8 years ago

Same problem here, get this message in every certain interval. Most annoying thing is I send some data in query parameter, based on which I send Server-Sent Events, so when the browser throws net::ERR_INCOMPLETE_CHUNKED_ENCODING and tries to reconnect to the URL, the request gets duplicated which I don't want. Tried end() method of stream to notify the browser that the transmission is completed but did n't work. The chrome guys saying browser looks for terminal 0-length chunk to decide that data transfer is completed, else retries after some interval. But here I saw the issue is closed without solution as removing "compression": {} from server/middleware.json doesn't work (I was able to get the data only after removal of "compression": {} but the issue is there). Please help.