在物联网中,有2个通信API –
- 基于REST的通信API
- 基于Web套接字的通信API
可以使用REST原理或使用Web套接字协议来实现Web服务–
1.基于REST的通信API:
代表性状态转移(REST)是一组体系结构原则,通过这些原则,您可以设计着重于系统资源以及如何寻址和转移资源状态的Web服务和Web API。 REST API遵循请求-响应通信模型。 REST体系结构约束适用于分布式超媒体系统中的组件,连接器和数据元素。
2.基于Web套接字的通信API:
Web套接字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 eiether 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 IP address and port number to retrive the data. | It depends upon the HTTP methods to retrive 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 require memory and buffers to store the data. |