Generally, we used two APIs for IoT communication APIs are:
1. Rest based communication APIs
Representational state transfer (REST) is a set of architectural principles by which you can design web services and Web APIs that focus on system's resources and how resource states are addressed and transferred.
REST APIs follow the request response communication model.
REST architectural constraints apply to components, connectors, and data elements within a distributed hypermedia system.
![restAPI]()
REST- based communication APIs constraints
Client-server : The principal behind the client-server constraint is the separation of concerns. For example client should not be concerned with the storage of data which is concerned of the serve. Similarly, the server should not be concerned about the user interface, which is not concern of the client. The Separation allows client and server to be independently developed and updated.
Stateless: each request from client to server must contain all the information necessary to understand the request; and cannot take advantage of any stored context on the server. The session state is kept entirely on the client.
Cacheable: Cache constraints requires that the data within a response to a request be implicitly or explicitly levelled as cache-able or non-cacheable. If a response is cache-able, then a client is given the right to reuse that response data for later, equivalent requests. Caching can partially or eliminate some instructions and improve efficiency and scalability.
Layered System: layered system constraints limit the behaviour of components such that any component cannot see beyond the immediate layer to them. For example, the client cannot tell whether it is connected directly to the end server or two an intermediary along the way. System scalability can be improved by allowing intermediaries to respond to requests instead of the end server, without the client having to do anything different.
Uniform Interface: Uniform interface constraints require that the method of communication between client and server must be uniform. Resources are identified in the requests (by URIs in web-based systems) and are separate from the representations of the resources data returned to the client. When a client holds a representation of resources it has all the information required to update or delete the resource you (provided the client has required permissions). Each message includes enough information to describe how to process the message.
Code on demand: Servers can provide executable code or scripts for clients to execute in their context. This constraint is the only one that is optional.
A REST - full web service is a "Web API" implemented using HTTP and REST principles. REST is most popular IoT Communication APIs.
![clientserver]()
HTTP methods:
| Uniform Resource Identifier (URI) | GET | PUT | PATCH | POST | DELETE |
|---|
| Collection, such as https://api.ex ample.com/reso urces/ | List URIs and perhaps other details of the collection's members. | Replace the entire collection with another collection. | Not generally used. | Create new entry in the collection. The new entry's URI is assigned automatically and is usually returned by the operation. | delete the entire collection. |
| Element, such as https://api.ex ample.com/reso urces/item5 | Retrieve a representation of the addressed member of the collection , expressed in an appropriate Internet media type. | Replace the addressed member of the collection, or if it does not exist, create it. | Update the addressed member of the collection. | Not generally used. Treat the addressed member as a collection in its own right and create a new entry within it. | Delete the addressed member of the collection. |
2. WebSocket-based Communication APIs
WebSocket APIs allow bi-directional, full duplex communication between clients and servers.
WebSocket APIs follow the exclusive pair communication model. Unlike request-response model such as REST, the WebSocket APIs allow full duplex communication and does not require a new connection to be setup for each message to be sent.
WebSocket communication begins with a connection setup request sent by the client to the server.
The request (called WebSocket handshake) is sent over HTTP and the server interprets it is an upgrade request. If the server supports WebSocket protocol, the server responds to the WebSocket handshake response.
After the connection setup client and server can send data/messages to each other in full duplex mode. WebSocket API reduces the network traffic and latency as there is no overhead for connection setup and termination requests for each message.
WebSocket suitable for IoT applications that have low latency or high throughput requirements. So Web socket is most suitable IoT Communication APIs for IoT System.
![websocket]()
In this article we learned IoT communication APIs: REST (request-response) and WebSocket (bi-directional). Understand their principles, constraints, and suitability for IoT applications. Hope you gained some Knowledge!
Thank you for reading this Article!
You are a Great Learner !!