Closing WebSocket correctly (HTML5, Javascript)
Asked 07 September, 2021
Viewed 2.3K times
  • 61
Votes

I am playing around with HTML5 WebSockets. I was wondering, how do I close the connection gracefully? Like, what happens if user refreshes the page, or just closes the browser?

There is a weird behavior when a user just refresh the page without calling websocket.close() - when they return after the refresh it will hit the websocket.onclose event.

6 Answer