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

Show parent comments

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.

2

u/hashtagframework Jun 13 '19

Thanks, that's how I understood it. I usually implement long polling to stream messages and keep the connection alive as long as possible... I usually set it 5-10 seconds under the max execution time for front-end requests.

5

u/sephg Jun 13 '19

That sounds like a hand-rolled version of server sent events. I'd recommend just using SSE directly. SSE is which are supported already by almost all browsers. (All browsers when using a polyfill.)

4

u/koreth Jun 14 '19

For some reason I don't fully understand, SSE seems to have never become a widely-known thing in the web development community, even though it has widespread browser support and is supported by many popular server-side stacks. I bet a significant percentage of web applications that use WebSockets could have used SSE instead with no loss of functionality at all.

2

u/hashtagframework Jun 14 '19

Yeah, I'm the sole author of a high level web framework that simplifies that kind of stuff, and I've never used SSE before. My current streaming polling solution sends the size of each message, then reads just that many bytes and processes that message. SSE with \n\n message termination after a valid javascript object seems much easier, but there is obviously some magic going on to detect that, and necessary polyfill hacks to support older browsers. It wasn't hard to make an alternative that works everywhere with vanilla javascript, so I've stuck with that, and likely everyone else has a similar story.

I built a lot of tools in my framework for JSON-RPC clients and servers, and modifying them to work for SSE is going to be extremely simple, so I'll definitely be adding those features. WebSocket features would be a bit trickier to guarantee the same levels of support, so I'm still holding off.

→ More replies (1)

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.

2

u/sephg Jun 13 '19

One benefit of http2 is that it can multiplex all communication over a single TCP connection. So when establishing a websocket connection the browser has to open a new tcp connection and negotiate TLS again. I wish they got on and added websocket support to http2 so a websocket request could piggyback off the socket used to download the other resources on the page in the first place.

2

u/cogman10 Jun 13 '19

Websockets are meant to be somewhat long lived. I don't think it would ideal to push websockets communication over HTTP2, it would significantly complicate the HTTP2 standard (what goes first, a websocket packet or http response? How do you differentiate? What about multiple sockets?)

The tls handshake cost is ultimately peanuts for connections that are supposed to live > 10 seconds. It only matters when you are talking about many short lived connections, which defeats the purpose of websockets.

→ More replies (1)
→ More replies (1)

1

u/darksparkone Jun 14 '19

For example, using Amazon SQS...

2

u/skroll Jun 13 '19

Which provider?

9

u/hashtagframework Jun 13 '19

Google App Engine - Standard. I've been involved in a support ticket requesting Web Sockets there for over a decade, and within the last couple of weeks they finally added support for them in the Flex environment for some runtimes. I looked into the Flex environment in the past, but it didn't support something else that the standard environment supported, so I never switched. I think it cost more, too.

I'm very well versed in scaling and pricing applications that use long polling, but I haven't priced a comparable websocket solution at any significant scale. What would you expect to pay per month for a websocket backend that could support 50,000 concurrent connections? What would the stack be? Do you always have to support a long polling backup in case the client can't use websockets?

9

u/TheRedGerund Jun 13 '19

15

u/hashtagframework Jun 13 '19

Yup, I'm ready... only problem is AWS costs 10 times more for the same thing I'm getting from GAE. My next project is focused on websockets, so I'll be looking around again. I'd rather not splinter the front-ends, paying for a doubled-up websocket server for every existing front-end server.

3

u/SladeyMcNuggets Jun 13 '19

I’ve never actually used GAE, but use GKE extensively and have auto scaling websocket infrastructure running on it. Just stick a ingress like nginx-ingress for the public facing end and you should be up and running pretty quick. It’s obviously a bit more extensive than GAE, but it should work well if you take the time to learn k8s.

→ More replies (39)