I cannot square these three facts (??) in my mind:
The only way to know if a connection is dead is if sending data over it fails
The only way to know if sending data fails is if you don't receive an acknowledgement
Server sent events have no acknowledgement. They are a one-way push
So, how does a server ever know if a connection is dropped?
If I navigate to a website and initiate a WebSocket / SSE connection (a permanent data-stream), how does the server know if the connection is ever dropped if I then leave the page shortly afterwards?
Is it the case that these technologies do have an acknowledgement for each message sent, except it's not user-accessible?
If that is the case, then why not? Couldn't it be useful in server-code to know if a message was successfully sent?
But if there's no acknowledgement at all, how can a server ever know if a connection is dropped?
CodePudding user response:
Simple, when it does not receive any more messages from the client. For example, if the server does not receive a packet in a determined amount of time, then a timeout event occurs which makes the web server close that connection/socket. That is the case for connection-oriented protocols such as TCP.
Other protocols, such as UDP, are connectionless, which means that they do not have acknowledgment events. The packet is sent and the client does not care whether the server receives it or not.
To answer your following questions:
If I navigate to a website and initiate a WebSocket / SSE connection (a permanent data-stream), how does the server know if the connection is ever dropped if I then leave the page shortly afterwards?
When you leave the site, the client (your browser) will most likely start a connection-termination phase where it will send a number of packets to the server, and the server will acknowledge that request by closing the connection.
Is it the case that these technologies do have an acknowledgement for each message sent, except it's not user-accessible?
See the previous answer. HTTP, the most prevalent web protocol does have acknowledgment packets, both to start the connection and to terminate it (also to make sure that the server/client got the data).
If that is the case, then why not? Couldn't it be useful in server-code to know if a message was successfully sent?
The server knows whether the message was successfully sent as long as it is a connection-based protocol.
But if there's no acknowledgement at all, how can a server ever know if a connection is dropped?
If no acknowledgment is used, then there's no connection established. It's a connectionless protocol (such as UDP). In this case, the server will receive the message (or not) and send the data without establishing a connection. The socket would be closed as soon as the data was sent.
Finally, you seem to have the facts mixed up. It all depends on the protocol used. Some establish connections, requiring that the client/server explicitly terminate the connection, or it will timeout. Others will just send the data and hope to get a response.