The foundation of the internet lies in the communication between computers. Computers, acting as servers, own and can provide resources to other computers, serving as clients. The roles of computers shift over time – one moment, a computer may own resources, while at another time, it may require resources from others. Communication is essential for data exchange between them. The predominant protocol facilitating Client-Server communication is the HyperText Transfer Protocol (HTTP).
HTTP is a protocol for fetching web pages, documents, images, media files, binaries, etc. Pretty much the whole of the internet runs on HTTP. We have discussed HTTP in detail here. A typical HTTP request flow is as follows:
- A client opens a connection and requests a resource from a server.
- The server calculates the response
- The server sends the response to the client on the same opened connection
- Polling / Short Polling / AJAX Polling
- Long Polling
- Webhook
- Server-Sent Events
- WebSocket
1. Polling / Short Polling / AJAX Polling
The client requests data from the server at regular intervals and if it is available, the server returns a response, otherwise, it returns an empty response. It involves making repetitive HTTP requests with a shorter time gap between each call. The flow is as follows:
- The client makes a request to the server.
- The server responds either with the data itself or an empty response
- After receiving the response from the server, the client will wait for the specified interval and repeat the previous actions.
Advantages:
- Short polling is easier to implement on both the client and server sides. If simplicity is a priority and real-time updates are not critical, short polling may be preferred for its straightforward implementation.
- In environments where legacy browsers lack support for modern techniques like WebSockets, short polling might be a fallback option. It can work in most web browsers without requiring additional technologies or server configurations.
- Short polling can be easier to debug and test, as it follows a more traditional request-response model. This simplicity in debugging might be beneficial during the development phase.
Limitations:
- Short polling may result in unnecessary server processing and bandwidth usage, especially when the client often receives responses indicating no new data. This inefficiency can be problematic in terms of resource utilization.
- Short polling may face challenges in large-scale systems with a high number of clients. The constant polling from a large number of clients can strain the server and hinder overall system performance.
- Frequent polling, even when there are no updates, can lead to increased battery consumption on mobile devices.
2. Long Polling
- The client makes a request to the server.
- If the data is available on the server, it responds with the data itself, otherwise, the server holds the connection until the data is available or a timeout occurs.
- Once the client receives a response from the server, it makes a new request without waiting.
Advantages:
- Long polling involves making periodic requests to the server, but these requests are less frequent compared to short polling. This can result in reduced server load, resource consumption, and decreased network traffic.
- Long polling can be more scalable than short polling, especially when there are many clients. The longer duration of each request reduces the frequency of incoming requests, easing the server load.
- Long polling requests often have a longer duration, making them less likely to be blocked by firewalls that may terminate short-lived connections. This improves the likelihood of successful communication in certain network environments.
- Long polling requests have a defined duration, making it easier to predict when the next request will be made. This predictability can be advantageous in certain applications.
Limitations:
- Although long polling reduces the frequency of requests, the open connections may lead to increased resource usage on the server, particularly in scenarios with a high number of long-lived connections.
- Long polling maintains an open connection for an extended period, which can result in increased connection overhead, especially when dealing with a large number of clients. This may impact server scalability.
- While Long polling allows the server to push data to the client, the client still needs to initiate a new connection for each update. It may not be as efficient as true bidirectional communication.
3. Webhook
- Registration: The client registers or subscribes to a particular event or set of events on the server. This involves providing a callback URL (webhook URL) where the server should send notifications.
- Event Occurrence: When the specified event occurs on the server (e.g., a new message in a chat application, a new order in e-commerce, a new comment on a social media post, etc), the server initiates an HTTP request to the callback URL.
- Data Payload: The server includes relevant data in the HTTP request payload, providing details about the event or changes that triggered the webhook. It sends the payload in the language understood by both parties, usually JSON or XML.
- Client Handling: The client's server receives the incoming HTTP request, processes the data payload, and takes appropriate actions based on the event. This could involve notifying users, updating data, or triggering further processes.
Advantages:
- Webhooks eliminate the need for frequent polling by delivering updates only when there is new information. This efficient use of resources reduces unnecessary network traffic and server load.
- Webhooks follow an event-driven architecture, enabling clients to respond to specific events without continuously querying the server. This makes the communication model more responsive and tailored to relevant events. It ensures real-time or near-real-time updates to clients, allowing them to receive information as soon as events occur on the server.
- Webhooks facilitate bidirectional communication by allowing servers to initiate communication and push data to clients. This is particularly valuable for real-time applications and instant updates.
- Webhooks can be implemented in a scalable manner, allowing a large number of clients to receive updates without straining server resources. This makes them suitable for applications with varying workloads.
Limitations:
- Ensuring the security of webhooks is crucial. If not properly implemented, webhooks may be susceptible to unauthorized access or malicious use. Proper authentication mechanisms are essential to mitigate security risks. Let's understand the risk with a hypothetical scenario:
- A user subscribes to the chat application's webhook by providing a callback URL (webhook endpoint) where notifications will be sent. The user's server is responsible for handling incoming webhook payloads.
- The chat application's server does not implement strong authentication or proper protection mechanisms for its webhook endpoint. The endpoint is publicly accessible, and there is no verification of the source of incoming requests.
- An attacker identifies the unprotected webhook endpoint and crafts a fake HTTP request, mimicking the format expected by the chat application. The attacker sends this fake request to the user's webhook endpoint, pretending to be the chat application server.
- The attacker successfully injects malicious or unwanted data into the user's chat application through the unprotected webhook.
- Webhook payloads are subject to size limitations imposed by the server or network infrastructure. Large payloads may be problematic, leading to issues with data delivery.
- Changes in the payload structure or behavior of webhook notifications may pose challenges, especially if clients and servers are not aligned with the same version. Proper versioning practices are needed to address compatibility issues.
Webhook Real Life Applications:
- Webhooks are used by Payment Systems to notify merchants or users about the status of transactions, allowing them to update order statuses or take necessary actions.
- E-commerce platforms utilize webhooks to notify sellers or administrators of new orders, changes in inventory, or other critical events, enabling timely action.
- Monitoring systems use webhooks to send instant alerts to administrators or relevant teams when there are issues or changes in system health, ensuring a quick response.
- IoT devices utilize webhooks to send updates about their status, sensor readings, or any significant events to central servers or applications.
Comments
Post a Comment