1

I'm testing SSE in my Rails app (server: Puma) in Chrome but they are not triggered:

setTimeout((function() {
  console.log("log1");
  var source = new EventSource('/websites/21/backlinks/realtime_push');
  source.addEventListener('pagination', function(e) {          
    console.log("log2");
    var data = JSON.parse(e.data);
    $('#pagination').html(data.html);
  });
}), 1);

only "log1" is written to console.

In developer tools I see XHR requests every time server pushes something (each second) but the response is empty - not sure if developer tools just don't show it or something else is wrong.

curl http://localhost:3000/websites/21/backlinks/realtime_push

returns:

event: pagination
data: {"html":"pagination"}

event: pagination
data: {"html":"pagination"}

so the data should be sent back...

What could be the problem here?

UPDATE: the problem is this monkey patch for problems with render_to_string from the question here: ActionController::Live with SSE not working properly

"this only appears to fix the problem... although it will cause the controller to actually send the data, the receiving end in JavaScript for some reason still won't get notified of events"

It's strange because in both cases when I use render_to_string and when I don't, I get the same headers with curl:

HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-UA-Compatible: chrome=1
Content-Type: text/event-stream
Cache-Control: no-cache
X-Request-Id: 4de7c8a6-a54f-45ef-9013-0447f85438c2
X-Runtime: 0.033030
Transfer-Encoding: chunked

But in one case it works on the JavaScript side and in other it doesn't :/

Community
  • 1
  • 1
davidhq
  • 4,660
  • 6
  • 30
  • 40
  • Yes, content-type is ok... I'm not creating EventSource every second, I'm pushing data through channel every second from the controller. – davidhq Nov 10 '13 at 16:09
  • ok... actually I'm not sure if setTimeout is necessary - I got that from somewhere... the entire thing is in `$(document).ready()` – davidhq Nov 10 '13 at 16:22
  • Threaded server - yes, I'm using Puma.. What exactly do you mean by "persistent connections" - do I have to do something special? I believe ActionController::Live is automatically using persistent connections... curl for example stays connected and is receiving a message every second until I quit it. – davidhq Nov 10 '13 at 16:29
  • ok, so Chrome doesn't show the response correctly.. I tried to test the thing in Safari and Firefox and it's even worse.. I don't see any requests :( – davidhq Nov 10 '13 at 16:46
  • I updated the question with my finding that the culprit is the fix for some other problem that apparently fixes the problem at the server-side, but at the receiving JavaScript end it causes new problem. – davidhq Nov 10 '13 at 23:43
  • This doesn't solve the problem but in case you are on a schedule you might try writing `JSON.dump` directly to the output stream. That's what I do, because in the past I had various problems with `render_to_string`, too. (Note: I deleted all my older comments because they don't add any value to the question) – Daniel Rikowski Nov 11 '13 at 08:13
  • ok.. but how do I render a template and get it's output... or even better: would it be possible to call kaminari's "paginate" from controller... I'd also like to avoid render_to_string, but don't see how. Don't quite understand what do you mean by `JSON.dump` – davidhq Nov 11 '13 at 14:08
  • I don't know any other way to render a template or `paginate`, sorry. I guess it doesn't help, but I'm doing `response.stream.write "data: #{JSON.dump(my_object)}\n\n"` where `my_object` is as Ruby hash with the data I need. – Daniel Rikowski Nov 11 '13 at 14:24
  • ok, thank you... It isn't useful to me though... I think I'm going to use Faye, this is broken at the moment. And also I need it to work in IE – davidhq Nov 11 '13 at 17:45
  • Possible duplicate of [Is it possible to use gzip compression with Server-Sent Events (SSE)?](http://stackoverflow.com/questions/23769001/is-it-possible-to-use-gzip-compression-with-server-sent-events-sse) – Paul Sweatte Jan 23 '17 at 17:03
  • I have similar problem in php... Everything works well, but the event is not being triggered. Changing innerHTML of div - no result. Sending an alert - no result. Anyone? –  Feb 23 '17 at 18:00

0 Answers0