Skip to main content

Socket.IO

Socket.IO is a JavaScript library for realtime web applications that enables realtime, bi-directional communication between web clients and servers. Socket.IO was created in 2010 and it was developed to use open connections to facilitate realtime communication, which was still a relatively new phenomenon at that time.

It works on every platform, browser or device, focusing equally on reliability and speed. It is a open source tool and a link to Socket.IO open source repository can be found here.

Socket.IO consists of two parts:

  • Client-Side: it is the library that runs inside the browser
  • Server Side: It is the library for Node.js

Both components have a nearly identical API. Socket.IO primarily uses the WebSocket protocol with polling as a fallback option, while providing the same interface. Although it can be used as simply a wrapper for WebSocket, it provides many more features, including broadcasting to multiple sockets, storing data associated with each client, and asynchronous I/O.

There are also several client implementation in other languages, which are maintained by the community:

Why use Socket.IO?

Writing a real-time application with popular web applications stacks like LAMP (PHP) has traditionally been very hard. It involves polling the server for changes, keeping track of timestamps, and it is a lot slower than it should be. 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.

This means that the server can push messages to clients. Whenever we write a chat message, the idea is that the server will get it and push it to all other connected clients.

Key Features of Socket.IO:

  • It helps in broadcasting to multiple sockets at a time and handles the connection transparently.
  • It works on all platform, server or device, ensuring equality, reliability, and speed.
  • It automatically upgrades the requirement to WebSocket if needed.
  • It is a custom real-time transport protocol implementation on top of other protocols.
  • It requires both libraries to be used Client side as well as a server-side library.
  • IO works on work-based events. there are some reserved events that can be accessed using the Socket on the server side like Connect, message, Disconnect, Ping and Reconnect.

Installation:

Prerequisties:-

First of all, please make sure that Node.js is installed on your system. The current Long Term Support (LTS) release is an ideal starting point. At least Node.js 10 is needed, older versions are not supported anymore.

Installation:-

To install the latest release:

// with npm
npm install socket.io

// with yarn
yarn add socket.io

Additional Packages:-

By default, Socket.IO use the WebSocket server provided by the ws package.

There are 2 optional packages that can be installed alongside this package. These packages are binary add-ons which improve certain operations. Prebuilt binaries are available for the most popular platforms so you don't necessarily need to have a C++ compiler installed on your machine. although note that these packages are optional and the WebSocket server will fallback to the Javascript implementation if they are not available. More information can be found here.

  • bufferutil: Allows to efficiently perform operations such as masking and unmasking the data payload of the WebSocket frames.
  • utf-8-validate: Allows to efficiently check if a message contains valid UTF-8 as required by the spec.

To install these packages:

$ npm install --save-optional bufferutil utf-8-validate

A more detailed documentation on how to get started with Socket.IO can be found here.

What Socket.IO is not?

Although Socket.IO indeed uses WebSocket as a transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either. Also the Socket.IO library keeps an open TCP connection to the server, which may result in a high battery drain for your users.

Future of Socket.IO:-

On 9th March 2021, Socket.IO V4 was released. It is an API clean-up with several new features like the much-awaited support for Typed Events, Immutability, and several bug fixes. When looking at NPM downloads, Socket.IO use has been increasing but only gradually.

But on the other, Sockjs and WS have been steadily growing and have outpaced Socket.IO in NPM downloads.

This indicates that although use of sockets has increased, developers have chosen alternatives to Socket.IO.

References: