Passenger + socket.io + Nginx + chrome - disconnect event

391 views
Skip to first unread message

Johnny Eric Amancio

unread,
Sep 21, 2014, 10:22:15 PM9/21/14
to phusion-...@googlegroups.com
I created an application where i create rooms like a chat to stream text content. That works well when running without passenger. Actually on passenger too, there's only one strange thing happening. 

I open two tabs on the browser, in one I create a room, in another I sync the room. then i can emit contents from each other.

I've put my app to emit an event when the socket disconnect on the server. If the socket who created the room closes the browser tab then I emit that event inside the  - on('disconnect') - on server side to all sockets in that room that was closed, informing that the room was actually closed. Finally the client side emits an alert('room was closed').

The strange thing is that on passenger + chrome or safari, the alert is not fired right after i close the browser tab. It is only fired when i try to send a message to that room from the remaining client, after this I get the alert('room was closed'). But if I run it outside passenger, it works.

I appreciate any help with this.
Thanks in advance.


Hongli Lai

unread,
Sep 30, 2014, 11:09:21 AM9/30/14
to phusion-passenger
Let me double check this. I'll get back to you asap.
> --
> You received this message because you are subscribed to the Google Groups
> "Phusion Passenger Discussions" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to phusion-passen...@googlegroups.com.
> To post to this group, send email to phusion-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/phusion-passenger.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/phusion-passenger/eb83438b-27c0-49bc-8657-a5dec145f9ef%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Phusion | Web Application deployment, scaling, and monitoring solutions

Web: http://www.phusion.nl/
E-mail: in...@phusion.nl
Chamber of commerce no: 08173483 (The Netherlands)

Hongli Lai

unread,
Oct 1, 2014, 11:24:16 AM10/1/14
to phusion-passenger
I believe that what's happening here, is similar to this issue:
http://stackoverflow.com/questions/24017770/event-stream-request-not-firing-close-event-under-passenger-apache/24067051#24067051

Phusion Passenger acts as a TCP proxy between the actual client and
your app. The thing about sockets is that there are two ways to find
out whether a socket has been closed: either 1) by reading end-of-file
from it, or 2) by writing to it and getting an error.

In case of WebSockets, when Phusion Passenger has noticed that the
client has sent EOF, it stops forwarding any further client data to
the application (because there is no further client data). However, it
does not close the connection with the application yet, because the
client may have only *half-closed* the socket (stopped writing to the
server). It does not half-close the connection with the application
either, because Node.js's HttpServer library does not support
half-closing by default. Thus, Phusion Passenger takes the
conservative approach, and keeps the connection alive.

To check whether the client has also stopped *reading* from the server
(also a half-close), a write is necessary. This is why your app does
not notice that the browser tab has closed, until it has attempted to
write something.

This issue is not limited to Phusion Passenger. If you put your
Node.js app behind a load balancer or any other kind of reverse proxy,
then you could also run into the same issue. It depends on the
implementation choices of the proxy.

The standard solution is to regularly send "ping" messages, with the
purpose of checking whether the connection is alive. This is the
reason why the WebSocket supports ping frames.

Johnny Eric Amancio

unread,
Oct 4, 2014, 11:03:53 AM10/4/14
to phusion-...@googlegroups.com, hon...@phusion.nl
Thank you very much for the help, I am new to node js, socket.io, I only used passenger for rails.

I'll try what you suggested, but I think that is it. Thanks again

Hongli Lai

unread,
Feb 28, 2016, 4:25:41 PM2/28/16
to Phusion Passenger Discussions, hon...@phusion.nl
Update February 28 2016: We have found a solution, and Passenger 5.0.26 (to be released at the time of writing) and later supports forwarding half-close events which fixes the problem. Just use 5.0.26 and later and it'll work as expected.
Reply all
Reply to author
Forward
0 new messages