Unfortunately detecting client disconnects isn't particularly easy with SSE, 
given that it's a one-way communication channel (unlike WebSockets which are 
two-way).

Even just talking about disconnects, there are several different types of 
disconnect that can happen such as:

  1. The `EventSource.close()` method is called in the JS side, closing the 
connection from the client: 
<https://developer.mozilla.org/en-US/docs/Web/API/EventSource/close>
  2. The user closes the web browser/tab or navigates away
  3. The user loses network connection (e.g. loses 4G signal or WiFi signal, or 
unplugs their network cable)



All of these will behave in slightly different ways, and detecting them isn't 
particularly easy in some cases depending on keep-alives and such. You can 
probably catch an exception from the underlying `asyncdispatch.send()` call 
which actually transmits data to the client to catch most cases where the 
client disconnects though.

Reply via email to