Search code examples
javascriptruby-on-railsnginxredisserver-sent-events

SSE over https not working


I am having an issue with SSE over https, let me make it clear, I have an Nginx server that listen to https, and a Rails Puma in the back, So I have this in the js( coffescript ):

initStream = () ->
   rechargePayment.source = new 
    EventSource('/proggress/stream')

and in the rails controller I have:

  def stream
    response.headers['Content-Type'] = 'text/event-stream'
    redis = Redis.new
    redis.psubscribe(['proggress.refresh', 'heartbeat']) do |on|
      on.pmessage do |_pattern, event, data|
        response.stream.write("event: #{event}\n")
        response.stream.write("data: #{data}\n\n")
      end
    end
  rescue IOError
    logger.info 'Stream stoped'
  ensure
    logger.info 'Stopping Stream Thread'
    redis.quit
    response.stream.close
  end

When I try this over https, it does nothing, in the logs I only see the error: "Stream closed", but if I try it over http it works perfectly.

EDIT: I saw this: "The EventSource interface is used to receive server-sent events. It connects to a server over HTTP and receives events in text/event-stream format without closing the connection."

source: https://developer.mozilla.org/en-US/docs/Web/API/EventSource

So, does this mean that it works only in http?


Solution

  • So yeah, the issue was in the nginx, I had to put these settings:

    proxy_set_header Connection '';
    proxy_http_version 1.1;
    chunked_transfer_encoding off;
    

    and in the rails controller add this:

    response.headers['X-Accel-Buffering'] = 'no'