Many web application programming interfaces (APIs) rely on HyperText Transfer Protocol (HTTP) as the foundational protocol for data transfer. While HTTP excels in executing batch tasks asynchronously, it faces limitations in scenarios requiring real-time communication, such as chat, live streaming, and gaming. We outlined some HTTP-based techniques and their respective constraints in the following table:
Technique | Description | Limitation |
Short Polling | Request frequently for updates from the server after a fixed short interval. The server responds whether it has an update or not. | Sends too many unnecessary requests for updates. |
Long Polling | Request for updates from the server with the channel left open (based on some constraints), and the server responds when it has an update. | Uses two separate connections for sending data and receiving updates, thus leading to resource wastage. |
HTTP Streaming | HTTP streaming allows servers to stream bytes of data continuously over a single connection to the client while keeping the connection open. | Suffers due to half-duplex communication |
The table above highlights the necessity for a solution facilitating low-latency, full-duplex data transfer between the server and client. The connection must remain open indefinitely to mitigate the latency associated with repeated handshaking. This is where event-driven architecture comes into play, addressing these requirements. Let's delve into their definition and operational mechanisms.
Event-drive architecture (EDA) revolves around events that drive data and applications. This approach allows applications to respond to events without the need for client requests. By eliminating issues associated with short and long polling, EDA differs from the traditional REST API method, where data is requested at intervals. The following illustration differentiates between request-response and event-driven model:
In this paradigm, clients are informed about events without sending requests to the server. An example is Instagram notifying followers about a user’s post, reactions, and comments. Event-driven architecture operates asynchronously, enabling immediate action without waiting for responses from event listeners—an ideal choice when clients need timely notifications about backend changes.
EDA uses different protocols based on the nature of the events or application requirements for communication. We’ll be discussing the following protocols:
A webhook is a one-way, event-driven communication protocol between servers or applications. It allows applications to promptly send real-time information upon the occurrence of specific events. In this setup, the server (consumer) initiates a subscription to a particular topic by connecting to a specified HTTP/HTTPS endpoint on another application’s server (producer). Once subscribed, the server automatically receives notifications about any new events. The webhook protocol proves useful when two servers or applications need to communicate. Let's consider the following example:
In the illustration, the webhook protocol is used for real-time communication between an online store and a third-party shipping service. The store subscribes to order status updates on the shipping service, and when a customer places an order, the store’s server automatically notifies the shipping service server through a webhook. This ensures immediate updates on order status, enhancing the overall customer experience with prompt and efficient order tracking.
Server-sent events (SSE) is a unidirectional event-driven protocol that works over a single HTTP connection used for server-client (server push) event communication. A server push means that the specified client automatically gets data from the server after the initial connection has been set up (until the connection is closed). It excels in real-time updates, employing a text-based event stream for efficiency. SSE supports automatic reconnection, allows event-type differentiation for varied updates, and enjoys widespread support in modern browsers. With its simplicity and compatibility, SSE is advantageous for applications requiring continuous data streaming and instant notifications, making it an excellent choice for dynamic and interactive web applications like live feeds and chat systems.
In the illustration above, the client automatically gets notifications until the connection is terminated by the client or server. The SSE and webhook protocols are similar in functionality, except webhook is for server-server, and SSE is for server-client event notification.
WebSub is a publish-subscribe protocol facilitating real-time updates between web publishers and subscribers. It operates through hubs, acting as intermediaries, and follows an asynchronous model. Subscribers subscribe to topics through hubs, and the hub verifies subscription intent from the subscriber and sends updates from publishers to subscribers. This protocol is widely adopted for efficient and timely distribution of content updates, particularly in scenarios like news feeds and blogs.
WebSocket is an event-driven protocol that enables bidirectional communication between the client and servers. It differs from the protocols mentioned above because they target sending events only from the server side (unidirectional). Whereas in WebSocket protocols, communicating parties can exchange data whenever needed and without restrictions or waiting for the response of the previous request. Communication happens by establishing a TCP connection and sending a connection upgrade request to the server.
Quiz
Let’s say you’re making a security system that makes decisions based on the notifications it gets. The following are the requirements:
It should verify when a consumer tries to subscribe to a publisher for the required notifications.
It should allow to customize and target the notifications to the consumers.
Which event-driven protocol is the most effective for this scenario?
Server-sent events (SSE)
WebHooks
WebSub
HTTP, while effective for asynchronous tasks, faces limitations in real-time scenarios. Event-driven architecture addresses this gap, enabling applications to respond to events without client requests. Protocols like WebSocket, WebSub, SSE, and Webhooks offer diverse solutions for real-time communication needs, enhancing data exchange efficiency between servers and clients.
Free Resources