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