WebSocket API
Understand the concept of WebSockets and the WebSocket API in the AWS API Gateway.
We'll cover the following
What’s WebSocket?
The REST API works on the HTTP protocol, which is unidirectional. The client sends a request to the server and gets a response. However, the server can’t go back to the client.
We need reverse communication in several business scenarios, like a chat application. When one user types the message, it must show on the other client. Therefore, the server can’t wait for the client to make an API call to fetch any pending notifications. Instead, the server must initiate it.
REST API can’t support this reverse communication, so WebSocket protocol was defined to handle it. The AWS API Gateway has an option to build a WebSocket API that leverages this WebSocket protocol to enable bidirectional communication between client and server.
Connection ID
The intricacies of the protocol are beyond the scope of this course. On a high level, we can say that it works with a lasting connection between the client and server. We retain this connection even if there’s no data to transmit. Because the connection is always live, the client or the server can send a message whenever it’s available.
The API Gateway tracks this connection using a connection ID. Whenever a client invokes a WebSocket API, it creates a new connection and notes the connection ID. After this, any server-side code can invoke the API Gateway service to send a message to the client by using the connection identified by the connection ID.
We have another chapter dedicated to WebSocket connections, where we’ll dive deep to create a new chat application using the WebSocket API.
Architecture pattern
A typical implementation of Websocket API in AWS is as follows:
Get hands-on with 1200+ tech skills courses.