by winrid · 4y ago · 0 comments
At FastComments - we do both. We use WS, and then poll the event log when required (like on reconnect, etc).
Products that can get away with just polling should. In a lot of scenarios you can just offload a lot of the work to companies like OneSignal or UrbanAirship, too.
If you're going to use WS and host the server yourself, make sure you have plans for being able to shard or scale it horizontally to handle herds.
It was hard for us to not use websockets, since like 70% of our customers pick us for being a "live" solution for live events etc.
This is what SSE (Server Side Events) is designed to do. A stripped down version of websockets which:
- auto reconnects
- exposes an optional offset
- only allows server -> client streaming
SSE is also supported by most web browsers.
SSE is a simple protocol that you can easily implement yourself both in the server and the client if the client lacks support.
SSE also adds naturally to existing infrastructure of request-response if you only use SSE for notification and keep everything the same, i.e. use same endpoints as before for fetching new data on a SSE notification event, and thus can be turned off as easily if problematic, e.g. too high server load.
IMHO, this unfortunate assumption is not really defensible in $current_year - especially from the multi billion dollar Cloud industry. I'd much more prefer first class support for long-lived connections on an infrastructure level, as opposed to a "proprietary database-level". I don't buy the argument that it's infeasible to solve the thundering herd issues.
[edit]
I looked it up and SSE was a much earlier standard, but implementation of WS and SSE were relatively contemporary with the exception of Opera (had SSE in 2006) and IE (Never got SSE support).
It does feel like websockets tried to cram several novel features whereas SSE was simply giving proper clothing to the existing art of long polling.
In particular, WS is binary encoded, has support for multiplexing/message splitting, several optional http headers, which in hindsight appears to have simply complicated the spec at little-to-nil value.
You can also use this API for integrations, and your clients/consumers will thank you. For example, our third party integrations use the event log to sync back to their own data stores. They probably call this every hour, or once a day. You wouldn't want to use websockets with PHP apps like WordPress.
Ideally, you should be able to poll because it is resilient. The challenge however is when you separate the initial poll/pull from the update stream because now you have to maintain two code paths. What I'm proposing is that the poll and update stream use the same data format using patching.
Overall we don't have much code since our websocket server is just nginx.