Back to blog
May 10, 2026·8 min read·Insights

Petroleum in the industry

If you are reading this, you are probably interested in creating an application or service that needs two-way communication that either side can initiate. Node JavaScript (Node.js) can be used to quickly develop and deploy this application using WebSockets, which were designed with this use case in mind.

AdminReviewed by Editor
womansippingcoffee

If you are reading this, you are probably interested in creating an application or service that needs two-way communication that either side can initiate. Node JavaScript (Node.js) can be used to quickly develop and deploy this application using WebSockets, which were designed with this use case in mind.

cre

What is a WebSocket?

A WebSocket is a computer communications protocol providing duplex communication channels over a single TCP connection. In other words, it allows internet-capable devices to communicate with each other, one acting as the client and the other acting as the server, with both able to initiate communication.

What types of applications use WebSockets?

The WebSocket protocol is used wherever a solution requires real-time communication and example use cases include:

WebSockets create a TCP socket connection between multiple devices or processes. As will be discussed later, similar functionality can be implemented using HTTP Long Polling or a hosted pub/sub service, but let’s build a simple example first using WebSockets and Node.js.

Is Node.js good for WebSockets?

Yes, Node.js is a great choice for implementing websockets as it provides a fast and scalable server-side environment that supports the popular ws library.

Why we use Node.js for WebSockets?

WebSockets require a persistent connection between the client and the server, which means that the server needs to be able to handle a large number of concurrent connections. Node.js uses an event-driven, non-blocking I/O model which makes it well suited to handle the resource requirements of WebSocket-based applications.

How to implement WebSockets in Node.js

The following guide will walk you through creating a WebSocket server in Node.js and show how you can connect to it using a WebSocket client running in the browser.

How many WebSocket connections can a Node.js server handle?

Production applications might handle tens of thousands of WebSocket connections simultaneously but the exact number will depend on several factors such as server hardware resources, the network bandwidth and the complexity of the application logic.

Node.js WebSocket API Example — a basic chat application

All source code associated with this blog is hosted on GitHub

To build a basic chat application with WebSockets, you will need both a client and server component.

For the server, we will use Node.js, and the client-side code will run within a web browser such as Chrome.

Node.js WebSockets Code (Server)

This application will require both a web server (HTTP server) and a WebSocket server (wss). The web server allows us to load our client (running in a browser), and the WebSocket server handles the bidirectional chat messages.

How to create a websocket server in NodeJS?

Create the Node.js app and install both the Express.js and ‘ws’ packages which will provide our web server and WebSocket server, respectively.

npm init
[Follow the prompts and accept the defaults]
npm install --save express
npm install --save ws

The web server portion will serve a single web page to the client, websocket-client.html, on port 3000:

const express = require('express')
const webserver = express()
 .use((req, res) =>
   res.sendFile('/websocket-client.html', { root: __dirname })
 )
 .listen(3000, () => console.log(`Listening on ${3000}`))

A WebSocket server can be created in only a few lines using the Node.js WebSocket library (ws) package:

const { WebSocketServer } = require('ws')
const sockserver = new WebSocketServer({ port: 443 })

Writing Websocket events for NodeJS examples

After creating an instance of a WebSocket server and specifying the port to run on, we can define any action that should happen after the WebSocket connection is established. In our case, we write connection events to the console and forward any messages we receive to previously connected clients. WebSockets are designed to run on the same ports as HTTP/HTTPS (i.e., 80 and 443).

sockserver.on('connection', ws => {
 console.log('New client connected!')
 ws.send('connection established')
 ws.on('close', () => console.log('Client has disconnected!'))
 ws.on('message', data => {
   sockserver.clients.forEach(client => {
     console.log(`distributing message: ${data}`)
     client.send(`${data}`)
   })
 })
 ws.onerror = function () {
   console.log('websocket error')
 }
})

And with that, the server portion is complete. Every time a message is received on any socket, it proxies the message to every connected client, which is the basis of any group-chat app.

#saas

Comments

Leave a comment

Name and email are optional. If omitted, you will appear as an anonymous guest. All comments are reviewed before publishing.