Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use WebSockets vs REST APIs?

I'm making a small website. It has a realtime chat page, accounts and signing in,a home page, and a blog. For the entire site I've been using websockets for communication, but I've heard that REST apis are better for handling things that don't need to be realtime. I'm planning on refactoring things to make it more optimal, but I'm not sure what traffic should be using http requests and what should be using websockets.

If I need websockets for some features, is it better to just use it for everything?

like image 476
CoolElectronics Avatar asked Nov 22 '25 18:11

CoolElectronics


1 Answers

As maybe you already know, the main difference between websockets and HTTP is the way of communication, so you should use the http protocol when you don't need the exchange of resources in real time, like update the profile of an user or authenticate in the system, or when you want a more robust way to handle errors, as HTTP provides several response codes. If you use websockets everywhere, soon you will notice that even if it works it doesn't seem right in some cases, So IMHO, just use websockets when you really need it, like with a chat or events that need to be delivered as soon as possible.

I'm gonna left a good article for you: https://blogs.windows.com/windowsdeveloper/2016/03/14/when-to-use-a-http-call-instead-of-a-websocket-or-http-2-0

like image 75
Yago Biermann Avatar answered Nov 24 '25 07:11

Yago Biermann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!