Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How server knows to close the connection of websocket when website tab is closed

I created a websocket (with perl Net::WebSocket::Server but I think it does not matter). My question is that when I close the website tab(not necessarily the whole browser) the server will disconnect the specific socket (my disconnect event is called). How the server manage to know that? I can not find a straight detailed description.

like image 946
Tonys Ansonī Misirgis Avatar asked Oct 18 '25 10:10

Tonys Ansonī Misirgis


1 Answers

If the browser closes one end of a socket (either explicitly via close, or implicitly because the process exits) the server at the other end of the socket connection will be notified that the socket is now closed. That's just part of how TCP works. Even if the message got lost and the server thought the socket was still open, when it tried to send data it would eventually work out that the other end was not acknowledging the packets and would drop the connection.

In the case of a browser tab, it's reasonable to assume that the browser will cleanup/close any resources associated with a tab that is being closed.

like image 129
Grant McLean Avatar answered Oct 21 '25 04:10

Grant McLean