8

I start developing a software, app coded using html + js I need to send this app notification from the server (java code) the app using nginx for routiong and is hosted in AWS. I investigated this subject of real time notification and I get confused between web sockets to long polling In what situations would AJAX long/short polling be preferred over HTML5 WebSockets?

In some articles I read that long polling is an old unlike websocket which is newer and better (In what situations would AJAX long/short polling be preferred over HTML5 WebSockets? ) I start inspect element of gmail facebook whatsapp web pages. I saw that Gmail+ facebook using long polling Unlike whatsapp which using Websocket. So why these companies still choose to use long polling? https://www.quora.com/Does-Facebook-use-WebSockets-for-any-of-their-applications-Are-they-really-useful-at-that-scale-especially-since-they-impose-a-stateful-architecture

Community
  • 1
  • 1
Ortal Blumenfeld Lagziel
  • 2,415
  • 3
  • 23
  • 33
  • It's all a matter of opinion and what environments you support. Older browsers and some servers don't support web sockets so in those cases you need long polling. – Mike Cluck Mar 29 '16 at 16:59
  • companies may not wish to upgrade some technology due to the cost of upgrade. But websocket is indeed "better" than long polling. less data transmitted, and communication can be initiated both ways. Take .NET SignalR as example. It checks if browser supports websocket. if it does, it uses ws, if not it falls back to longpolling – Ji_in_coding Mar 29 '16 at 17:01
  • 1
    The socket.io library will detect if webSockets are supported on both ends and, if so, use that. If not, it will use long polling. Older services probably got things working with long polling and just don't see a need to re-engineer now, but if designing from scratch now for modern browsers, webSocket would be the usual choice. – jfriend00 Mar 29 '16 at 17:49
  • most backends that don't support websockets can do SSE (EventSource), which is better than long-polling under most rubrics. Reasons not to use Sockets include limited legacy compat, tying up the same server port for a long time (SSE/comet can pool), that some public hotspots (ex hotels) don't transport WebSockets correctly, tooling around Sockets is under-developed (compared to http: logs/auth/middleware/gzip/etc), and that Sockets are two-way while a given project only needs to push out events. – dandavis Mar 29 '16 at 17:49
  • One major deficiency in sockets ATM: if someone send()s the server a 5gb blob, there's no easy way in most packages to 1. tell how big that blob is, and 2. cancel the transfer without dropping the connection. Also, Sockets tend to live in a large pool where if one client crashes/overloads his connection, it affects other users, whereas php+apache+SSE won't "crash big" like that. – dandavis Mar 29 '16 at 17:53

2 Answers2

6

A couple of reasons why some companies are still using long-polling:

  • WebSocket support is still not 100%, even though the legacy browsers without support are slowly dying out. So if you're a company like Google where products have to work on pretty much every browser out there, you still need a non-WebSocket fallback solution.
  • If you already have a working solution then the costs of moving to WebSocket may well outweigh the savings it brings.
gzost
  • 2,375
  • 1
  • 18
  • 25
  • people forget about embedded systems. Many are not upgradable, and do not support websockets. Websocket is great, but if you are making something that needs to be compatible with older systems, depending on the application, polling a REST API is good enough – Dani Feb 07 '18 at 21:55
  • Is this true even as of 2020? Or can we assume WebSockets are supported by almost all devices out there? Thank you! – tonix Oct 11 '20 at 12:27
-2

WebSockets it's better, it just drains less power, the client does not have to be asking for content every X time, the only downfall it's that old browsers does not support them.

You have a full comparasion in this other question if you want to have more information.

In what situations would AJAX long/short polling be preferred over HTML5 WebSockets?

Community
  • 1
  • 1
Luis Tellez
  • 2,785
  • 1
  • 20
  • 28
  • 1
    "Better" is not really an apropos term to compare two different things: is a gun better than a knife? – dandavis Mar 29 '16 at 17:56