Socket.IO

Socket.IO

Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. Sockets have traditionally been the solution around which most real-time chat systems are architected, providing a bi-directional communication channel between a client and a server.

 

Socket.IO is composed of two parts:

  • A server that integrates with (or mounts on) the Node.JS HTTP Server socket.io
  • A client library that loads on the browser side socket.io-client.

 

The main idea behind Socket.IO is that you can send and receive any events you want, with any data you want. Any objects that can be encoded as JSON will do, and binary data is supported too. Socket.IO includes a heartbeat mechanism, which periodically checks the status of the connection. And when the client eventually gets disconnected, it automatically reconnects with an exponential back-off delay, in order not to overwhelm the server. Socket.IO provides a convenient way to send an event and receive a response.


If you use plain WebSockets for your application, you will eventually need to implement most of the features that are already included (and battle-tested) in Socket.IO, like reconnection, acknowledgements or broadcasting.Only closing the underlying HTTP server is not sufficient, as it will only prevent the server from accepting new connections but clients connected with WebSocket will not be disconnected right away. Sets a modifier for a subsequent event emission that the event data may be lost if the clients are not ready to receive messages (because of network slowness or other issues, or because they’re connected through long polling and are in the middle of a request-response cycle). The Socket class inherits from EventEmitter. The Socket class overrides the emit method, and does not modify any other EventEmitter method.

To view or add a comment, sign in

More articles by Sharmin Akter

  • Unit Test

    Unit testing is a critical part of software development, and it involves the testing of individual code units to ensure…

Explore content categories