After my ActionController::Live listens for about 5 minutes, I get the following in my browser and my EventSource drops the connection.
EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.
Any idea where that 'text/html' request is coming from?
I'm using Unicorn and Nginx.
This is my backend code:
def listen_to_checkins
response.headers['Content-Type'] = 'text/event-stream'
redis = Redis.new
redis.psubscribe("checkin.*.#{current_branch.id}") do |on|
puts "subscribed..."
on.pmessage do |pattern, event, data|
puts "got message..."
response.stream.write("event: checkin.create\n")
response.stream.write("data: #{data}\n\n")
end
end
# rails 4.2 uses ClientDisconnected instead of IOError
rescue ClientDisconnected
logger.info "Stream closed"
rescue IOError
logger.info "Stream error"
ensure
redis.quit
response.stream.close
end
end
And this is my coffee:
ready = ->
if !source
source = new EventSource('/validator/listen_to_checkins')
source.addEventListener 'message', (e) ->
console.log "got message"
source.addEventListener 'checkin.create', (e) ->
console.log "got back: #{e.data}"
get_last_checkins()
source.addEventListener 'open', (e) ->
console.log "opened"
source.addEventListener 'error', (e) ->
console.log "error"
Okay.. I ended up reconnecting on error on the client's side. I hope this helps someone until I find a more elegant solution.
ready = ->
if !source
reconnect()
reconnect = ->
if ($("meta[name=listen_to_checkins]").attr("content") == "true")
console.log("reconnecting")
source = new EventSource('/validator/listen_to_checkins')
source.addEventListener 'message', (e) ->
console.log "got message"
source.addEventListener 'checkin.create', (e) ->
console.log "got back: #{e.data}"
show_checkins(JSON.parse(e.data))
source.addEventListener 'error', (e) ->
# reload page on error if we're listening to checkins
setTimeout ()->
reconnect()
, 1000
console.log "error"
console.log e
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With