gblblgl gblblgl - 2 months ago 11
Python Question

How do I notify Python/Tornado that the client has closed the tab/browser?

I've been searching for quite a while for a solution about this but no dice.

Edit: I didn't point out that I'm trying to make a chat server. So people log in, their id gets appended to a

users
and a
listeners
list. And they start chatting. But when one of them tries to close the tab or browser the user will never be deleted out of both lists, so he/she stays logged in.

Edit2: I thought that the numbering above was a little confusing so I posted the part in the script as well at the bottom.

So far I've tried the on_connection_close() function (which doesn't ever get fired, I don't know why), the
on_finish()
function (which gets fired every time when a
finish()
is called) so that doesn't fit the bill either.

Now I've come up with a little bit of a solution which involves the
on_finish()
function:


  1. Whenever the
    UpdateHandler
    class'
    post()
    function gets called then
    self.done = 0
    is set.

  2. Just before the
    finish()
    function gets fired I set
    self.done = 1
    .

  3. Now the
    on_finish()
    function gets called and I print
    self.done
    on the console and it's 1.

  4. In the same
    on_finish()
    function I do an
    IF
    self.done = 1
    statement, as expected it returns
    TRUE
    and Tornado's
    io_loop.add_timeout
    with the parameters
    time.time()+3
    (so that it sleeps for 3 seconds to make sure if the user navigated to another page within the website or completely went away from the website) and the callback that eventually is going to be called.

  5. After the 3 seconds I want to check whether
    self.done
    still equals 1 or if the user is still on the website then sure enough it will be 0.



btw, every 30 seconds the server finishes the connection and then sends the user a notification to initiate a new connection so that the connection never times out on it's own.
When the client closes the browser and the 30 second long timeout expires then the server tries to send a notification, if the client was still on my website then it would initiate a new connection thus calling the
post()
function in the UpdateHandler class I mentioned above thus setting the variable
self.done
back to 0. (That's why I gave the
io_loop.add_timeout
a margin of 3 seconds.)

Now that that's taken care of I wanted to go ahead and try and see how it works.
I started the server and opened up a browser navigated to the right url and watched how the server responded (by placing a few
print
statements in the script). When the user stays connected I can see that after the post() call (which shows at that time
self.done = 0
) it sleeps for 3 seconds, and then the callback function gets called but this one prints
self.done = 1
which is strange.

I know this is not the most efficient way but it's the only solution I could come up with, which didn't even work as expected.

Conclusion:

I hope someone has a good alternative or maybe even a point in my theory that I missed which breaks the whole thing.
I really would like to know how to let Tornado know that the client closed the browser without waiting for the 30 second timeout to finish.
Maybe with pinging the open connection or something. I looked into
TORNADIO
for a little bit but didn't like it that much. I want to do this in pure Tornado if it's possible of course.

I'll submit the code ASAP, I've been trying for like half an hour looking at 'How to Format' etc. but when I try to submit my edit it gives an error.


  • Your post appears to contain code that is not properly formatted as
    code. Please indent all code by 4 spaces using the code toolbar
    button or the CTRL+K keyboard shortcut. For more editing help, click
    the [?] toolbar icon.


Answer

I've been having this issue for like 5 - 6 days and finally found out what the problem is, well.. not exactly actually but it's solved! I've been searching on the internet but found nothing. I told in the above post that I do remember it working when I tried the same script a couple months ago, but I never mentioned using nginx back then. I've been struggling with Apache + mod_proxy but I don't know what the issue is with apache but when I tried nginx this time again it just worked!

If you have the same issue (on_connection_close not getting fired) "TRY" nginx. Thanks for your help too @Nikolay.