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

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]

0

u/[deleted] Jun 13 '19

[deleted]

72

u/Doctor_McKay Jun 13 '19

Connect again.

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.

1

u/stephenlblum Jun 14 '19

Arguably long polling is similar to websockets except where you reconnect after every message that is sent to the client.

Re-establishing the TCP connection each message will be inefficient. Long-polling systems should maintain the TCP connection while sending/receiving messages. Long-polling systems should leverage the subsequent subscription requests as message receive receipts to acknowledge the receipt of a message. Long-polling systems should use HTTP/2.0 for full duplex support with one TCP connection.

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.

1

u/[deleted] Jun 15 '19

[removed] — view removed comment

1

u/hashtagframework Jun 15 '19

Do you use read receipts to confirm messages are received? Is that built into websockets? When the websocket reconnects, so you need to flush the entire state, or how do you deal with lost messages?

1

u/Doctor_McKay Jun 13 '19

Any modern browser, even on mobile, supports websockets. So if you know your setup supports it then no need to support polling.

A lot of people don't really know this. Chances are, if a client can handle your CSS then they have support for WebSockets.

3

u/JokerSp3 Jun 13 '19

Some of our customers are behind corp proxys that block websockets :(

1

u/NoInkling Jun 14 '19

Websockets over port 80 didn't work on my old DSL modem/router for some reason (yes I know these days everything should be over TLS anyway), I tried everything to make it work. Caused me issues with certain sites at the time.

1

u/loopsdeer Jun 14 '19

Not my Kindle Paperwhite's experimental browser :'(

1

u/minusthetiger Jun 13 '19

Only if you want to support long polling failover.