Search⌘ K
AI Features

WebSocket API

Explore how WebSocket API supports asynchronous, bidirectional communication for real-time applications within AWS serverless computing. Understand routing with route keys, persistent connections, and how to implement secure chat apps using API Gateway, Lambda, and DynamoDB to manage connection IDs and message broadcasting.

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.

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.

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 ...