A brief introduction about Sockets programming
September 08 2020 · 5 min read
Sockets.IO is based on a web sockets API that allows a tunnel for direct communication with client-side and server-side.
So the server can't send messages on its own to the client. The browser has to must refreshed and the message is requested from the server. However, sockets.io allows for instantaneous communication.
In long-polling, the client will send AJAX like a request to the server. With a suitable timeout, the server would send back a response if there was an update. Here the client will continuously or periodically send request updates from the server, Through separate TCP connections, congesting network traffic.
In short-polling, the client periodically sends a request to the server asking if there's anything new. The server doesn't wait, but rather sends back if there's an update or just an empty message. Here the network gets even more congested with these continuous requests, even when there are no updates.
WebSockets can be regarded as the future of client-server communication. In WebSockets, there would be one TCP connection between the client & server at all times. There's the bi-directional data flow between client & server as well as real-time nature due to always open TCP connection.
Socket.io can also be used in real-time communication, This means without sending the request to the server-client will get the updated date. By using socket.emit on the server-side and to catch the request socket.on at the client-side.
How can we use it
Socket.Io can be used to get real-time updates from the server. How about the old days before sockets.io, we have to refresh our browser to get updated messages or data. How about time-consuming and the internet, with the help of sockets we get real updates nowadays everyone is using social media and we get real-time notifications when a user comments on a post or liked a post without refreshing the app or loading it again.
Which types of apps use sockets?
Sockets are widely used in chat applications like or customer care support we get a real-time update when we message a friend and the message is rendered in his UI without refreshing his app, and when he typing we also see that he is typing a reply for us, What about youtube when a user comments on video comment will shows on comment section without refreshing our browsers, there are lots of apps using web sockets popular among them or Facebook Messenger WhatsappWechat.
- Reliability: It can establish a connection even in the presence of proxies,load-balancers, personal firewalls, and antivirus software.
- Auto-reconnect Support: Unless mentioned explicitly in the code, the client library will try to reconnect forever, until the server is available again.
- Disconnection detection: It allows both the server and the client to know when the other one is not responding anymore.
- Multiplexing support: It allows us to have several communication channels on the same underlying connection.
- Binary Streaming support: It also allows emitting any serializable binary data likeArrayBuffer, Blobs, etc.