Polling and Streaming
So far we have seen systems where client request some peice of information from the server and the server responds with the data. What if we are building a systems where the client will be able to monitor the temperature outside, the temperate is going to change a lot, and the client should be able to view these changes. Similarly we are buiding a system with a chat app where client that are meant to communicate with each other in real time. How can build a system to reflect the regularly updated peices of data. This is where polling and streaming comes handy.
Polling
- The idea behind polling is that client would request a data from the server on periodic basis following a set interval ( every x seconds ).
- Now poling has limitation especially when considering the situation of chat room, if need instantaneious or live data, then polling is not useful as polling only happens after a fixed interval. Now we reduce this time that elapses such as request data every 0.1 second and eventually this would seem instantaneous, however this remedy comes with a downside. Now every client makes a polling request 10 times in one second, and if this was large application with 1000s of users issuing 10 request every seconds can increase load signifantly on the server. This is where streaming becomes useful.
Streaming
- Instead of having the client repeatedly request data from the server, the client can open a long-lived connection with the server. This is done with the help of socket. The socket is a file that lives on your computer that your computer can write to and read from to communicate with another computer, in a long lived connection sort of way. Essentially a portal through which one computer can communicate with another. Here the client is streaming data from the server. In the previous sections, we saw the server responding to client’s request, but here the client has an open and long-lived connection to the server and keeps waiting for the data from the server. So server is much more proactive in that regard and sending data continous stream of data.
- Now we can have that instantaneous experience instead of sending 10 request/client for example but we can have one long lived connection for transfering data. Now although it may seem like steaming is more superious to polling however this is not the case. Depending on the user-case, the polling may be better, this depends on the system we are designing. If we need some instantaneous data such as live updates,chat then streaming is better but if we building a monitoring application, in that case polling would be btter as this can be done every 30seconds for example.
back