On server-side using Sinatra with a stream
block.
get '/stream', :provides => 'text/event-stream' do
stream :keep_open do |out|
connections << out
out.callback { connections.delete(out) }
end
end
On client side:
var es = new EventSource('/stream');
es.onmessage = function(e) { $('#chat').append(e.data + "\n") };
When i using app directly, via http://localhost:9292/
, everything works perfect. The connection is persistent and all messages are passed to all clients.
However when it goes through Nginx, http://chat.dev
, the connection are dropped and a reconnection fires every second or so.
Nginx setup looks ok to me:
upstream chat_dev_upstream {
server 127.0.0.1:9292;
}
server {
listen 80;
server_name chat.dev;
location / {
proxy_pass http://chat_dev_upstream;
proxy_buffering off;
proxy_cache off;
proxy_set_header Host $host;
}
}
Tried keepalive 1024
in upstream
section as well as proxy_set_header Connection keep-alive;
in location
.
Nothing helps :(
No persistent connections and messages not passed to any clients.