Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSockets vs XHR for data transfer

I am designing an architecture for a web application using Node.js, and we need to be able to send medium size files to the client from a gallery. As a user browses the gallery, they will be sent these binary files as fast as possible(for each gallery item). The files could go up to 6Mb, but probably average around 2Mb.

My client is insisting that we should use websockets for data transfer instead of XHR. Just to be clear, we don't need bi-directional communication.

I lack the experience in this domain and need help in my reasoning. My points so far are the following:

  • Using WebSockets breaks any client-side caching that would be provided by HTTP. Users would be forced to re-download content if they visited the same item in the gallery twice.
  • WebSocket messages cannot be handled by/routed to proxy caches. They must always be handled by an explicit server.
  • CDNs are built to provide extensive web caching, intercepting HTTP requests. WebSockets would limit us from leveraging CDNs.
  • I guess that Nodejs would be able to respond faster to hundreds/thousands of XHR than concurrent websocket connections.

Are there any technical arguments for/against using websockets for pure data transfer over standard HTTPRequests. Can anyone nullify/clarify my points and maybe provide links to help with my research?

I found this link very helpful: https://www.mnot.net/cache_docs/#PROXY

like image 903
Philip Taylor Avatar asked Aug 09 '16 02:08

Philip Taylor


1 Answers

Off the top of my head, I can see the following technical arguments for XHR besides that it uses HTTP and is therefore better at caching (which is essential for speed):

  • HTTP is the dedicated protocol for file downloads. It's natively built into browsers (with the XHR interface), therefore better optimised and easier to use for the developer
  • HTTP already features a lot of the things you'd need to hand-craft with websockets, like file path requests, auth, sessions, caching… All both on the client and server side.
  • XHR has better support even in older browsers
  • some firewalls only allow HTTP(S) connections

There do not seem to be any technical reasons to prefer web sockets - the only thing that might affect your choice is "the client is king". You might be able to convince him though by telling him how much he has to pay you to reimplement the HTTP features on a websocket connection. It ain't cheap, especially when your application gets more complex.

Btw, I wouldn't support your last point. Node should be able to deal with just as many websocket connections as HTTP connections; if properly optimised all things are even. However, if your server architecture is not based solely on node, there is a multitude of plain file serving applications that are probably faster than node (not even counting the HTTP caching layer).

like image 112
Bergi Avatar answered Sep 24 '22 12:09

Bergi