How does Twitch count views?

Theres multiple ways to do it and I’m not sure which twitch uses exactly but if you’re looking to do something similar and fishing for a solution, then there are onbeforeunload events that browsers fire when you close them. Whether its closing the tab, or the whole browser, they fire an event signaling it which allows developers to handle these exits however they please. In addition, a session can be created and managed by the server, the life of the session could be very short and is kept alive by the constant streaming of data/http requests. When the user exits, the scripts that kept the session alive don’t run since nothing is being streamed and the server then kills the session. When the session dies it removes it from the viewer count. Twitch could also use its own propriety method but its most likely one of these or a combination of both.