Server side using Sinatra with stream block.
get '/stream', :provides => 'text/event-stream' do stream :keep_open do |out| connections << out out.callback { connections.delete(out) } end end
On the client side:
var es = new EventSource('/stream'); es.onmessage = function(e) { $('#chat').append(e.data + "\n") };
When I use the application directly, through http://localhost:9292/ everything works fine. The connection is permanent and all messages are sent to all clients.
However, when it passes through Nginx, http://chat.dev , the connection is dropped and the reconnection is triggered every second or so.
Nginx setup looks fine:
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; } }
I tried keepalive 1024 in the upstream section, as well as proxy_set_header Connection keep-alive; in location .
Nothing helps: (
There are no persistent connections and messages that are not transmitted to any clients.
ruby nginx sinatra
Lukas Mayer Dec 02 2018-12-12T00: 00Z
source share