Remix.run Logo
Arch-TK 5 days ago

Surely if you want to reliably do some action when a user leaves your page then some persistent connection which dies is by far the most reliable option...?

I'm no frontend expert but cant you just open a websocket and when the websocket dies from the server's perspective, you have your cue to do whatever cleanup you wanted to do?

I also agree with the others here that there shouldn't really be a _need_ for this kind of feature.

Etheryte 5 days ago | parent | next [-]

Keeping a websocket alive would work, but then you're stuck holding a websocket for all of your users for the full session. Fine if you need a socket anyway, pretty wasteful if you don't. Sockets are pretty cheap these days, but I wouldn't really give this serious thought for a page with a lot of users.

Arch-TK 4 days ago | parent [-]

Holding a socket is very cheap. Unless websockets introduce some overhead I am not aware of.

bilekas 5 days ago | parent | prev | next [-]

> I'm no frontend expert but cant you just open a websocket and when the websocket dies from the server's perspective, you have your cue to do whatever cleanup you wanted to do?

If I'm not mistaken, and I very well might be, it's kind of the same, however a WS connection here may be less reliable, infact if the user has a spotty connection, when you think "That user left", in the case of a local cleanup, okay that could run on the client side, however this beacon seems to be aimed at sending data back to the server when you leave, in the case of a dropped connection, you wont be able to send the data back anyway to the server.

For this 'browser' based implementation under the hood, the browser *should* have more insight into the real intention of the users exit.

Arch-TK 4 days ago | parent [-]

I really wouldn't want to be doing anything that requires reliability on the back of either solution as user agents can just opt to never send beacons.

afavour 5 days ago | parent | prev [-]

You're right that a persistent connection is the most reliable option. But it's also significantly more difficult to scale and will cause battery drain on mobile devices. The Beacon API is a better alternative for not-entirely-essential cases like analytics.

Arch-TK 4 days ago | parent [-]

Scale wise, from the served side, it these are inactive TCP sessions at the end of the day. There isn't a serious scalability problem that doesn't exist with beacons, except that you might need to create some new software to support this.

A persistent connection when a device is awake, as long as you're not sending data over it, will be nothing compared to the screen power draw.

On the other hand, most phones will aggressively unload your app when the screen goes off or when its not in focus thereby killing your socket.

I don't think there are actually many downsides to this as long as (afaict this is the case) websockets are just HTTP layer sockets downgraded to plain TCP.