Answer the question
In order to leave comments, you need to log in
Nginx timeout error when using SSE Rails?
Hello, I'm trying to set up a server and use SSE. Here is an example of an error that occurs:
2016/08/24 16:15:57 [error] 6923#0: *1427 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 178.35.63.179, server: _, request: "GET /users/sse/events HTTP/1.1", upstream: " http://unix:/var/run/unicorn.sock/users/sse/events ", host: "example.ru", referrer: "example.ru/1"
Here is the controller function that manages SSE:
def events
response.headers["Content-Type"] = "text/event-stream"
ActiveRecord::Base.connection_pool.with_connection do |connection|
conn = connection.instance_variable_get(:@connection)
begin
conn.async_exec "LISTEN user#{current_user.id}"
conn.async_exec "LISTEN heartbeat"
conn.wait_for_notify do |event, id, data|
puts event.to_s
if event == "user#{current_user.id}"
response.stream.write("event: message\ndata: #{data}\n\n")
elsif event == "heartbeat"
response.stream.write("event: heartbeat\ndata: heartbeat\n\n")
end
end
rescue
#Client disconnected
ensure
conn.async_exec "UNLISTEN user#{current_user.id}"
conn.async_exec "UNLISTEN heartbeat"
conn.flush
conn.finish
end
end
rescue
#puts "@@@@@@@@@@@@@@ IOERROR"
ensure
#puts "@@@@@@@@@ Strem closed"
response.stream.close
end
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question