When we want to build such an app as a text messenger, we need a tool that will handle real-time communication with the server. In this article I will describe just the tool that will help us do this.


What is Socket.IO?

Socket.IO was created in 2010 by Guillermo Rauch. Socket.IO is a JavaScript library that allows real-time communication with the server. Socket.IO allows two-way communication between the client and the server. To establish a connection and exchange data between the client and server, Socket.IO uses Engine.IO, which is a lower-level library than Socket.IO. Engine.IO is used to implement the server, and Engine.IO-client is used for the clients.


Capabilities of Socket.IO

Socket.IO provides the ability to implement real-time analysis, binary streaming, instant message sending and receiving. Socket.IO establishes connections even in the presence when the user is using proxy, system firewall and antivirus software. By default, it also supports automatic reconnection, as well as detects the disconnection of the client from the server and also the other way around, the server is informed if the client is disconnected. Using this library, the developer does not need to know how to use the WebSocket protocol to use Socket.IO.


Using Socket.IO in practice

The following example connects Socket.IO to an HTTP server listening on port 3000.

const server = require('http').createServer();
const io = require('socket.io')(server);

io.on('connection', client => {
  client.on('event', data => { /* … */ });
  client.on('disconnect', () => { /* … */ });



The connection using Express JS.

const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);

io.on('connection', () => { /* … */ });


Related articles

Show all articles