DEV Community

Cover image for WebSocket Vs. REST
Shridhar G Vatharkar
Shridhar G Vatharkar

Posted on

WebSocket Vs. REST

Every day, we get asked, "What is the difference between WebSockets and the REST API?" With this post, we hope to cut through the jargon surrounding these technologies and provide a straightforward explanation of how they work.

The purpose of this tutorial is to help the reader decide whether WebSockets or REST is better suited to their data needs. Before we start into WebSocket and REST API applications, let's first define them.

What exactly is a WebSocket?

WebSocket is a computer communication protocol, or "a collection of rules" that allows two computer machines to communicate with one another. It allows two computers to communicate bidirectionally over a TCP Socket.

Let's look at how WebSocket can aid with data delivery. A WebSocket, in essence, provides a continuous two-way connection between the client (requesting data) and the server (sending data).

One example is a phone call, where once connected, it allows for continuous two-way communication. You can find a more extensive explanation of WebSockets.

What exactly is a REST API?

REST is a set of constraints or design rules for developing web services that use the HTTP protocol to interact between the client and the server. But the connection is always unidirectional. As a result, the REST API only provides data upon request.

It is similar to a vending machine in that you must ask for the appropriate resource in order to obtain one.

What is the purpose of WebSocket?

WebSocket is commonly used for continuous and duplex data delivery, such as low latency and high-frequency forex data feed.

The client will be resource-intensive if it requests 100s of rates per second over REST, whereas the WebSocket client can get 100s of rates per second without being resource-intensive.

What are the functions of the REST API?

Where data demands are not constant and stateless, REST API is used. For example, you may request current data when your application requires it, or you could request historical timeseries data to produce a chart.

REST is a resource-based idea similar to our vending machine example; however, Amazon.com is a more fitting extension of that analogy because you can request your resource online and have it delivered anywhere.

When should you utilize WebSockets and when should you use REST?

WebSockets vs REST is more of a "what is more appropriate?" question than a "what is best?" inquiry. REST is more adaptable and can supply nearly all of the resources we give.

It is useful when data needs are ad hoc, but it is ineffective when data delivery needs to be ultra rapid (more than 10 requests per second) and continuous, where every tick rate counts.

To Summarize

WebSockets and REST are both technically clever, which explains their current popularity. The best option is determined by the user's data requirements. We hope this has improved your comprehension of both delivery options; but, if you still have questions, please contact one of our specialists right away.

Also, take a look at the originally published tutorial: WebSocket Vs. REST

Top comments (1)

Collapse
 
yet_anotherdev profile image
Lucas Barret

Cool article ! I found this really well explained that REST and WebSocket are not really comparable. Yes it is for communication purpose but that's it.
It is not for the same needs and even one is a protocol the other one is pattern.
Thanks !