Performance differences between HTTP REST API and WebSocket REST API
What is an API?
An application programming interface (API) is a piece of software that provides an interface for other software to interact with it.
What is a REST API?
REST or RESTful API design (Representational State Transfer) is an architectural style that uses existing protocols. There are six key constraints for REST API design:
- Using the unified interface (UI)
- Client-server based
- Stateless Operation
- RESTful Resource Caching
- Layered system
- Encoding on demand
What is HTTP REST API?
While REST can be used with almost any protocol, it usually leverages HTTP. So basically an HTTP REST API is an API that uses the HTTP protocol to transfer request/response data between the server and the client while using REST API design principles.
What is a WebSocket REST API?
This is an API that uses the WebSockets protocol (built on top of the HTTP protocol) to transmit request/response data between the server and the client, while using REST API design principles.
Why is this important for responsiveness?
While WebSockets might be overkill when used for just a single request (open web socket -> send request -> get response -> close web socket), they are very fast when we perform multiple requests on a single connection. The whole idea behind WebSockets is to establish a bidirectional communication channel between the client and the server. This means two things:
- This type of connection allows us to receive updates from the server without sending any requests (for example, we can create notifications)
- Because we create a connection only once and use it multiple times (for as many requests as we want), we save a lot of server resources and bandwidth, so we get faster responses.
Performance Testing and Results
The hardware used to perform the tests is an Apple MacBook Pro 16" with an M1 Max processor and 32GB RAM and an iPad mini 4. The server was created inside a VM (2 cores, 4GB ram). NodeJS (v.18.2 .0) was used to create the same “book” mock data for both HTTP REST API and WebSocket REST API. The response length was 358 bytes.
For our testing purpose, we created a simple Flutter application (v3.0.1) to play the role of a testing tool. I chose the Dio package to create HTTP requests and test the HTTP RESP API part and the web_socket_channel package for the WebSocket REST API part. The results of multiple requests are in milliseconds.
Final Thoughts
From the table and graph above, we can see, unsurprisingly, that WebSockets gain more and more ground as the number of requests increases, regardless of whether we are testing on an emulator or a real device.
In my next article, we will explain how to build a simple NodeJS server architecture with API versioning for a WebSocket REST API.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to redirect a website from HTTP to HTTPS
Publish Date:2025/03/16 Views:117 Category:NETWORK
-
HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi
How to Fix the “SSL Handshake Failed” Error (5 Methods)
Publish Date:2025/03/16 Views:96 Category:NETWORK
-
Installing a Secure Sockets Layer (SSL) certificate on your WordPress site enables it to use HTTPS for a secure connection. Unfortunately, there are a lot of things that can go wrong in the process of verifying a valid SSL certificate and e
Detailed introduction to Let's Encrypt
Publish Date:2025/03/16 Views:129 Category:NETWORK
-
Let's Encrypt is a free, automated, and open certificate authority that officially launched in April 2016. It was originally founded in 2012 by two Mozilla employees. Their goal for founding Let's Encrypt was really simple; to encrypt the e
HTTP2 Tutorial - The Past and Present of HTTP2
Publish Date:2025/03/16 Views:73 Category:NETWORK
-
HTTP was originally proposed by Timberners-Lee, a pioneer of the World Wide Web, who designed the application protocol with simplicity in mind to perform advanced data communication functions between web servers and clients. The first docum
HTTP2 Tutorial - The shortcomings of HTTP1.1
Publish Date:2025/03/16 Views:145 Category:NETWORK
-
HTTP 1.1 is limited to handling only one outstanding request per TCP connection, forcing browsers to use multiple TCP connections to handle multiple requests simultaneously. However, using too many TCP connections in parallel can cause TCP
HTTP2 Tutorial - HTTP2 Functional Upgrade
Publish Date:2025/03/16 Views:87 Category:NETWORK
-
Before we officially introduce the functions of HTTP/2, let's take a detour to understand the past and present of HTTP/2 . Multiplexing Streams The bidirectional sequence of text-formatted frames sent via the HTTP/2 protocol exchanged betwe
HTTP2 Tutorial - How to use HTTP/2 with HTTPS
Publish Date:2025/03/16 Views:84 Category:NETWORK
-
HTTPS is used to build ultra-secure networks connecting computers, machines, and servers to handle sensitive business and consumer information. HTTP/2 browser support includes HTTPS encryption, which actually complements the overall securit
HTTP2 Tutorial - How to Configure HTTP2 with Nginx
Publish Date:2025/03/17 Views:195 Category:NETWORK
-
HTTP2 was officially released in 2015. If your website is still using HTTP/1.1, you may be out of date. Don't worry, here we will see how to use Nginx to upgrade your website to HTTP2. Install Nginx I feel that this column is redundant. Sin
OAuth2.0 - A comprehensive understanding of OAuth2.0
Publish Date:2025/03/17 Views:72 Category:NETWORK
-
When I first came into contact with OAuth2.0, I often confused it with SSO single sign-on. Later, due to work needs, I implemented a set of SSO in the project. Through my gradual understanding of SSO, I also distinguished it from OAuth2.0.