Day 19 — Real-Time Communications | WebSockets vs SSE vs Long Polling
আমার development journey শুরু হয়েছিল PHP দিয়ে। আর স্বাভাবিকভাবেই জানেন যে PHP দিয়ে data fetch করাতে হলে প্রত্যেক বার HTTP request করতে হতো। তখন আমি first time Facebook-এর messenger-এর chatting feature দেখে অবাক হয়েছিলাম। কোনো reload ছাড়াই message send এবং receive হচ্ছে। এই বিষয় টা নিয়ে একটু research করার পরে জানতে পারি এইটা asynchronous communication। Page reload ছাড়াই server request পাঠানো যায়।…
Day 19 of the development journey saw a deep dive into real-time communications, specifically comparing WebSockets, Server-Sent Events (SSE), and Long Polling.
Before this exploration, PHP was the primary tool for data fetching, requiring an HTTP request on each occasion. This led to the discovery of asynchronous communication through Facebook Messenger's chat feature, which allowed messages to be sent and received without page reloads. This asynchronous communication is achieved through HTTP requests, similar to those used with fetch or axios, but with a notable limitation: server-side messages are not received without a page reload.
The desire for two-way communication led to research on WebSockets, a method that enables always-on connections between server and client, allowing messages to be sent and received without page reloads. Unlike HTTP requests, WebSocket connections remain open, providing a constant channel for communication.
However, WebSockets are not without their drawbacks. For instance, in a game leaderboard scenario where client-side input is minimal, using WebSockets could be inefficient as it enables two-way communication even when only one-way communication is needed. This inefficiency arises from the need for servers to handle unnecessary requests, potentially leading to increased server load and bandwidth usage.
As an alternative, Short Polling was considered. This method involves periodic requests to the server to check for updates. While it offers a solution to the problem of WebSockets for scenarios with minimal client-side input, it still has its issues. For example, it leads to unnecessary requests even when no updates are available, increasing server load and bandwidth usage.
Long Polling, another option, improves upon Short Polling by having the server hold the client's request until new data arrives. However, this method also has its downsides, such as increased server memory usage when handling concurrent requests from many clients.
The final solution explored was Server-Sent Events (SSE), a one-way push method where the server sends data to the client without requiring client input. This approach is suitable for scenarios where only server-side updates are needed. For instance, in a scenario where only server-side data needs to be pushed to clients, SSE would be a more efficient choice than WebSockets.
In conclusion, the choice between WebSockets, SSE, and Long Polling depends on the specific requirements of the application. Each method has its strengths and weaknesses, and the decision should be based on factors such as the need for two-way communication, server load, and bandwidth usage.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.