Why is SSE Connection Closing Unexpectedly with Ruby on Rails/JS?

Viewed 18

I have the following code to trigger a server side event that just relays the time (for testing purposes only).

  def check_record_status
    response.headers['Content-Type'] = 'text/event-stream'
    sse = SSE.new(response.stream, event: "deploymentRecordUpdate")
    puts sse
    begin
      sse.write({ :id => 1, :time => DateTime.now })
    rescue ClientDisconnected
    ensure
      sse.close
    end
  end

In addition, on the console, I run:

var source = new EventSource('/live_status');
source.addEventListener("deploymentRecordUdpate", function(event) {
  data = JSON.parse(event.data);
  console.log(data);
});

However, I notice that on my terminal where I am running the backend server, a GET request is sent multiple times to the controller. By my understanding, a server-side event should initiate a HTTP request once which is then maintained. Is there any reason the connection is unexpectedly closing, or is the expected behavior.

0 Answers
Related