SSE (Server-sent events) not working

那年仲夏 提交于 2019-12-23 03:07:18

问题


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 :/

来源:https://stackoverflow.com/questions/19890266/sse-server-sent-events-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!