r/programming • • Jun 13 '19

WebSockets vs Long Polling

https://www.ably.io/blog/websockets-vs-long-polling/
581 Upvotes

199 comments sorted by

View all comments

425

u/[deleted] Jun 13 '19

[removed] — view removed comment

274

u/sysop073 Jun 13 '19

Go figure, since they were basically invented to eliminate the need for polling

60

u/hashtagframework Jun 13 '19

Go figure, my web host doesn't support WebSockets in the auto-scale configuration I use, but Long Polling still works fine.

119

u/saltybandana2 Jun 13 '19

the only reason you would use long polling is being unable to use websockets in a reasonable manner.

13

u/hashtagframework Jun 13 '19

Do you always have to support a long polling backup in case the client can't use websockets?

0

u/[deleted] Jun 13 '19

[deleted]

16

u/hashtagframework Jun 13 '19

What about clients using VPNs or behind restrictive firewalls? I was more concerned about the network limitations. Does the WebSocket tunnel just like a normal TCP keep-alive HTTP request? Are they prone to disconnects?

0

u/[deleted] Jun 13 '19

[deleted]

→ More replies (8)

16

u/kryptkpr Jun 13 '19

The outside is wrapped in a GET that never completes, yes.

→ More replies (6)

4

u/sephg Jun 13 '19

Yes and yes. But you need a strategy / code for reconnecting anyway so it’s not that big a deal. Arguably long polling is similar to websockets except where you reconnect after every message that is sent to the client.

→ More replies (5)

3

u/psaux_grep Jun 13 '19

Lots of older security proxy solutions don’t work well with web sockets. Nginx handles it fairly well, but older versions of ISAM does not at all. Just passes the upgrade request along, but closes it so you can’t reply.

Using a library like socket.io enables you to leverage web sockets even when dealing with clients or proxies that can’t, but yes, you’ll end up actually using long polling, but at least you don’t need to implement it.

→ More replies (3)
→ More replies (4)
→ More replies (1)

3

u/martixy Jun 13 '19

Or HTTP2.

7

u/cogman10 Jun 13 '19

That solves a different problem ultimately.

Http 2 works great when you have a ton of resources you want to download or requests you want to make in parallel.

It does, however, still have somewhat of an overhead for each request and response.

Websockets have no such overhead.

Further, Http 2 really is still focused on request/responses. Http 2 allows for a server push, but the client doesn't have to recognize that push. This is a problem if you are, for example, doing something like a game. You want your client to update when new info comes down from the server, you don't want to be requesting info from the server every 10ms.

Websockets are for when you need bidirectional communication (chats, games, stock price updates) where the server is giving you information without you requesting it AND your client is responding to those messages without needing a poll loop.

All that being said, I can't think of many applications where you'd really need that. In server to server communication, a MQ system works much better. So that leaves server to browser communication. Most web apps simply don't need that sort of communication.

→ More replies (4)
→ More replies (1)
→ More replies (46)

4

u/paul_h Jun 13 '19

COMET (long polling) wasn't your grandfather's polling!

20

u/DrunkOnSchadenfreude Jun 13 '19

cries in restrictive corporate proxy

long polling it is then

6

u/[deleted] Jun 13 '19

This seems like a clear winner, but at what point would the server fall over from too many sustained connections? 10K 100K, 1M? wouldn't each websocket connection consume resources from the server that wouldn't be released until the client or server has terminated the connection?

And more importantly how wouldn't this be scaled behind the reverse proxy, would that cause an additional connection Client -> proxy -> web cluster host to be maintained as well?

3

u/Entropy Jun 14 '19

That would depend on how big the box is, and how efficent the web server running it is. Phoenix framework (Elixir on the Erlang vm) recently had something like 2 million simultaneous websockets running on a single large box.

Websockets are likely to be even more scalable in the future with HTTP3. You're making the kernel do a lot less work since it's UDP-based. Less syscall overhead (especially useful when running on hardware with spectre/meltdown mitigations in place).

2

u/masklinn Jun 14 '19

This seems like a clear winner, but at what point would the server fall over from too many sustained connections? 10K 100K, 1M? wouldn't each websocket connection consume resources from the server that wouldn't be released until the client or server has terminated the connection?

Depends on the size of the box, the software stack, the amount of work (per second per connection) and the amount of tuning.

Whatsapp was doing 3m on a single box back in 2012.

5

u/Fidodo Jun 13 '19

Finally! The answer to the question everyone already knew the answer to.

8

u/mmcnl Jun 13 '19

Webpolling is something for if you're still living in 2010.

5

u/duheee Jun 13 '19

2010

1996 you mean?

7

u/mmcnl Jun 13 '19

Nah, back in 2010 websockets wasn't as universally supported as it is these days.

→ More replies (2)

4

u/stfm Jun 13 '19

We used applets in 1996

5

u/eggn00dles Jun 13 '19

if you only need to poll your server like once every 30 minutes using websockets would be dumb af

→ More replies (4)