📜  Rest API 和 Web Socket API 的区别

📅  最后修改于: 2021-09-11 04:38:44             🧑  作者: Mango

在物联网中,有 2 个通信 API——

  • 基于 REST 的通信 API
  • 基于 Web Socket 的通信 API

Web 服务可以使用 REST 原则或使用 Web Socket 协议来实现——

1.基于 REST 的通信 API
REpresentational State Transfer (REST) 是一组架构原则,您可以根据这些原则设计专注于系统资源以及如何寻址和传输资源状态的 Web 服务和 Web API。 REST API 遵循请求-响应通信模型。 REST 架构约束适用于分布式超媒体系统中的组件、连接器和数据元素。

2.基于 Web Socket 的通信 API
Web Socket API 允许客户端和服务器之间进行双向、全双工通信。它遵循专有的结对通信模型。此通信 API 不需要为要在客户端和服务器之间发送的每条消息设置新连接。一旦建立连接,就可以不间断地连续发送和接收消息。 WebSocket API 适用于具有低延迟或高吞吐量要求的 IoT 应用程序。

Rest API 和 Web Socket API 的区别:

S.NO. REST API WEB SOCKET API
1. It is Stateless protocol. It will not store the data. It is Stateful protocol. It will store the data.
2. It is Uni-directional. Only either server or client will communicate. It is Bi-directional. Messages can be received or sent by both server or client.
3. It is Request-response model. It is Full duplex model.
4. HTTP request contains headers like head section, title section. It is suitable for real-time applications. It does not have any overhead.
5. New TCP connection will be set up for each HTTP request. Only Single TCP connection.
6. Both horizontal and vertical scaling (we can add many resources and number of users both horizontally and vertically). Only vertical scaling (we can add resources only vertically).
7. It depends upon the HTTP methods to retrieve the data.. It depends upon the IP address and port number to retrieve the data 
8. It is slower than web socket regarding the transmission of messages. web socket transmits messages very fastly than REST API.
9. It does not need memory or buffers to store the data. It requires memory and buffers to store the data.