9

Recently I dived into the topic of WebSockets and built a small application that utilizes them.

Now I wonder why HTTP based API'S are still being used, or rather, why they are still being proposed.

As far as I can see there is nothing I can't do with WS that would be possible via HTTP, but the other way round I gain a lot of improvements.

What would be a real world example of an application that takes more benefits from a HTTP powered backend than from a WS one?

user2422960
  • 1,476
  • 6
  • 16
  • 28

3 Answers3

6

@Julian Reschke made good points. The web is document based, if you want your application to play in the WWW ... it have to comply with the game rules.

Still, you can create WS based SPA applications that comply with those.

But still you want to use HTTP for some other things, like getting resources or views and cache them using HTTP cache mechanisms. For example, if you have a big application you want some big views to be downloaded on demand, rather than pack everything in a big main view.

It would be a pain to implement your own caching mechanism for HTML to get views and cache them in the local storage for example. Also, by using traditional HTTP requests, those views can be cached in CDNs and other proxy caches.

Websockets are great to maintain "connected" semantics, send data with little latency and get pushed data from the server at any time. But traditional HTTP request is still better for operations that can benefit from distribution mechanisms, like caching, CDN and load balancing.

About REST API vs WebSocket API (I think your question was actually about this), it is more a convenience than a preference. If your API has a big call rate per connection... a websocket probably makes more sense. If your API gets a low call rate, there is no point in using WebSocket. Remember that a Websocket connection, although it is lightweight, it means that something in the server is being held (ie: connection state), and it may be a waste of resources if the request rate do not justify it.

vtortola
  • 34,709
  • 29
  • 161
  • 263
5

Bookmarking? Page history? Caching? Visibility to search engines?

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
1

HTTP and WebSockets are two Web tools originated to accomplish different tasks. With HTTP you typically implement the request/response paradigm. With WebSockets you typically implement an asynchronous real-time messaging paradigm.

There are several applications where you need both the paradigms.

You can also try to use WebSockets for request/response and use HTTP for asynchronous real-time messaging paradigm. While the former makes little sense, the latter is a widespread technique, necessary in all the cases where WebSockets are not working (due to network intermediaries, lack of client support, etc.). If you are interested in thus topic, check out this other answer of mine, which tries to clarify the terminology related to these techniques: Is Comet obsolete now with Server-Sent Events and WebSocket?

Community
  • 1
  • 1
Alessandro Alinone
  • 4,934
  • 2
  • 18
  • 22