WebSocket API

Learn about WebSocket APIs and how we can configure them using AWS API Gateway in AWS.

Unlike REST API, which is used for synchronous communication, WebSocket API is used for asynchronous communication. It enables real-time bidirectional communication between client and server. In contrast to the request/response model, where the client sends the request and the server responds, WebSocket API allows the server to send one-way messages to the client.

Press + to interact
WebSocket connection vs. HTTP connection
WebSocket connection vs. HTTP connection

Bidirectional communication

But why do we need bidirectional communication when we have REST APIs? Consider a real time chat application as one shown in the figure below. Alice wants to send a message to Bob, so it sends a POST request to the server with the message payload. When the request arrives at the server, it does not know how to pass on the message to Bob. That’s when bidirectional communication and WebSocket API come in handy.

Press + to interact
REST API vs. WebSocket API
REST API vs. WebSocket API

WebSocket API handles such scenarios by using the publish-subscriber model. It establishes a persistent connection between the publishers and the subscribers. Clients subscribe to specific channels, and the ...