Deploy your apps to a supercloud in a few clicks

This Engineering Education program is supported by Section. Instantly deploy your GitHub apps, Docker containers or K8s namespaces to a supercloud.

Try It For Free


August 6, 2020

Websockets are becoming very common in the modern web development environment. Most web developers nowadays want to develop real-time web applications or customize their existing applications to become real-time in order to satisfy their users needs appropriately. This is where websockets play a major role.

What is a websocket?

Websocket is a computer communication protocol that enables full-duplex communication between the client and server over a single TCP connection. Websocket protocol which is located at layer 7 of the OSI model enables interaction between a web browser or other client applications and a web server with lower overhead than half-duplex alternatives such as http polling hence promoting real-time data transfer between the two.

What is socket. io? is a JavaScript library that enables real-time, bi-directional and event driven communication between the client and server. consists of two similar APIs i.e.

  1. A node.js server API.
  2. A JavaScript client API which can also be run from node.js. uses under the hood to establish connection and to exchange data between client and server. is used for the server side implementation whereas is used for the client side implementation. release version 1.0 which was it’s major release version was released on the 28th of May, 2014. The project contained two parts before version 1.0 i.e. a transport handling implementation and a high-level API. With the major release, transport handling has been moved to which allows developers to build new real-time APIs for the real-time web without reinvention. Apart from that, introduced the following changes:

  1. Binary streaming support.
  2. Improved horizontal scaling support.
  3. module to support node.js streams.

Setting up socket. io on the server side

Setting up on a node.js server is just as simple as adding event listeners to a http.server instance. Let’s look at a code example

const server = require('net').createServer()
const io = require('')(server)

io.on('connection', (socket) => {
    // new client connection
    socket.emit('connect', {message: 'a new client connected'})

server.listen(2400) can also be attached to other HTTP frameworks. The code snippet below shows how to attach to express.js framework.

const express = require('express')
const server = require('http').Server(app)
const io = require('')(server)

io.on('connection', (socket) => {
    // new client connection
    socket.emit('connect', {message: 'a new client connected'})


The code snippet below shows how to attach to a hapi framework.

const server = require('hapi').createServer(2400);
const io = require('')(server.listener);

io.on('connection', (socket) => {
    // new client connection
    socket.emit('connect', {message: 'a new client connected'})


Setting up socket. io on client side

For a client to connect to server, script needs to be added in the body tag of a the client html page. Here is a code example

<script src="/"></script>

The code snippet Below makes a connection to the server. It uses local host as an example.

        var socket = io.connect("http://localhost:2400");

Socket. io client-server communication is event based meaning that the client and server communicate through events. Both client and server can emit and listen to events and each event contains a body which in most cases is a JSON object. Below is an example tweet event emitted by a node.js server and listened by the client.

io.on("connection", (socket) => {
    const tweet = {source: 'Peter', text: "Hello, world!"};
    socket.emit('tweer', tweet)

This is how the client’s browser listens for the event

socket.on("tweet", (tweet) => {
    console.log("tweet from", tweet.source);
    console.log("content:", tweet.text);

We can send any JSON object to and from the server. This includes strings, numbers, arrays and booleans. We can also send Node.js Buffer objects starting with 1.0.


Socket.IO has played an important role in the popular adoption of Node.js by making WebSockets both accessible and reliable. Version 1.0 represents a major step in its evolution and the extraction of Engine.IO has opened up some interesting possibilities for the Node.js ecosystem.