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

111

u/masklinn Jun 13 '19 edited Jun 13 '19

It's really sad that in the LP/WS discussion, Server-Sent Events have been completely ignored / forgotten. The article mentions it but then goes on to ignore it entirely:

  • it's a unique "streaming" connection so server load & message ordering & al of longpolling are not problematic

  • but it's standard HTTP, you probably want some sort of evented system on the server end but that's about it, there's no connection upgrade or anything

  • and it automatically reconnects (except FF < 36 didn't)

  • and you can polyfill it, except on Android Browser

The drawbacks are:

  • it's one-way (server -> client), you'll need to do regular xhr from the client to the server and will have to handle the loop feeding back into SSE, whereas WS lets you get a message from the client and immediately write back on the sam connection

  • because it’s regular http the sse connection is drawn from the regular pool lowering concurrency (unless you use a separate domain)

  • for some insane reason it has worse browser support than webstockets, mostly because neither IE nor Edge support it natively (the polyfill works down to IE8 or something)

  • the polyfill requires 2KB of padding at the top for some browsers

  • the server needs to send heartbeats to keep the connection open

46

u/earthboundkid Jun 13 '19

When I learned about SSE I was surprised it doesn’t have more traction. Most sites only need a one way connection, eg for a live blog. Even for something like a chat client, users could send messages out on a separate channel and listen for new messages on a common SSE endpoint. Bizarre that no one talks about it.

16

u/MeisterD2 Jun 13 '19

It's used pretty commonly for writing online multiplayer games with HTML5 + JS, it's the only sane way to do netcode in that context, imo.

3

u/jf908 Jun 14 '19

What are the advantages of SSE for games compared to WebSockets?

3

u/MeisterD2 Jun 14 '19

SSE is good for asymmetrically updating the game client with new information (potentially spurred by other players interacting with server-side systems.) With minimal messaging overhead.

If you need real-time netcode, go with WebSockets. In THAT case, WebSockets are the only sane way to do netcode.

→ More replies (1)

9

u/cahphoenix Jun 13 '19

I use SSE in a realtime coolaboration app. In my testing and research they were lower latency, lower power consumption and a little easier to use and setup.

7

u/sephg Jun 13 '19

Re: IE support, since Edge’s engine is being internally replaced with chromium, edge will also support server sent events in the next version. At that point the only browser you really need a polyfill for is IE11. If you care about that.

https://caniuse.com/#feat=eventsource

6

u/johnfound Jun 13 '19

I am using SSE all the time with great success.

It is very easy for handling and definitely the best choice if you have asymmetrical traffic with more information flowing from the server to the client and only little information (requests) from the client to the server.

Which is the most common case anyway.

12

u/GuinnessDraught Jun 13 '19

My experience with SSEs has been that they are just a half-baked and more limited WebSocket. You handle the SSE stream nearly the same way you would a WS, but you don't get the full benefits of a 2-way protocol. I also had a harder time dealing with errors and connection resets with SSEs than I've ever had with WebSockets.

For the future if I need streaming I'd sooner just go full-on WS over SSE I think, unless there was a really specific reason not to. I don't doubt there are valid reasons to do so, but I can't think of one off the top of my head.

10

u/intuxikated Jun 13 '19

Haven't used either, but it seems like the people at TutaNota had the opposite experience, + improved battery life on SSE for their android app: https://f-droid.org/en/2018/09/03/replacing-gcm-in-tutanota.html This is however for an android app, not a browser app (used as google cloud messaging replacement)

10

u/Ravavyr Jun 13 '19

Makes sense. SSE doesn't require the app to do anything. The server pushes to it. Websockets has both client and server "aware" of the other and sending/getting pingbacks, so battery usage is going to be more.

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

4

u/rcfox Jun 13 '19

You probably need client-side heartbeats with websockets though too, depending on the application. If a client just disappears (if their Internet connection dies, for instance) the server-side connection can last for minutes.

I've also found that Heroku will kill websocket connections that it sees as idle.

→ More replies (6)

4

u/[deleted] Jun 14 '19

Another plus: the SSE spec can be read over a long lunch break. It's simple, easy to understand, easy to implement, plain HTTP, and robust. It really is a joy to work with.

10

u/[deleted] Jun 13 '19

SSE doesn’t support Authorization headers, which made it DOA for my purposes. What a pity - it would’ve been a perfect fit for job statuses, progress of processing, etc

6

u/[deleted] Jun 13 '19

Do Websockets support Authorization headers? I've been trying to figure that out in a spring-boot server for the last couple of days without reaching anything. Any helpful links would be great.

9

u/kryptkpr Jun 13 '19

No, user supplied headers are explicitly disallowed. Use query params to pass your tokens.

9

u/sickcodebruh420 Jun 13 '19

It's my understanding that tokens do not belong in query params. It's possible they'll be cached or logged.

3

u/AdrianTP Jun 13 '19

Agreed, but that should be less of an issue if you use a stateless token solution. Plenty of web API providers still exist which have you pass your token in the query params (though perhaps "other people do it" is a bad argument for why it's ok).

3

u/ScarletSpeedster Jun 13 '19

To workaround this generally I pass a JWT with expiration built in every time a socket is opened. If the JWT expired or is not authorized then they get dropped.

7

u/[deleted] Jun 13 '19

No, they don’t. Tried that as well.

You’ll literally have to either encode your auth info into the websocket path, or create a mechanism for generating a one-time websocket URL from something that does respect Authorization, or implement a phase after connecting via the websocket that demands authorization before doing anything else in the bidirectional channel.

6

u/Ravavyr Jun 13 '19

Well you don't need Authorization. SSE pushes from server to client. When client sends something it would be an ajax call possibly.

3

u/[deleted] Jun 13 '19

You do need that header if you’re streaming pushes to the client from a protected resource, like say from an API to drive a web app and you want consistency (Authorization: Bearer ...).

→ More replies (7)

2

u/trickyanswers Jun 14 '19

Author here. Grteat comment and you're right, we skim over SSE in this article. But we're adding support for SSE to our platform in the coming weeks and we've got some things around SSE in the pipeline. So we definitely haven't ignored / forgotten it!

→ More replies (1)