Innozee Node Websocket


 Implementing WebSocket connection using NodeJS

 by Innozee

 Posted on 13/12/2023



WebSocket is a communication protocol that provides a full-duplex ( offers you a two-way ) communication channel over a single, long-lived connection between a client and a server. It is designed to overcome some limitations of the traditional request-response model of HTTP (Hypertext Transfer Protocol). Here are key features and characteristics of WebSocket:


  1. Full-duplex Communication: WebSocket allows for bidirectional communication between a client and a server. Both the client and server can send messages independently at any time.

  2. Low Latency: WebSocket reduces latency compared to traditional HTTP connections because it eliminates the need for repeated connection establishment for each request-response cycle.

  3. Efficiency: Unlike HTTP, where each request from the client needs a corresponding response from the server, WebSocket enables the server to push data to the client without waiting for a request. This reduces the need for unnecessary polling.

  4. Persistent Connection: Once a WebSocket connection is established, it remains open for the duration of the session. This persistent connection is maintained to facilitate real-time communication.

  5. Low Overhead: WebSocket has a lower overhead compared to HTTP. The protocol is designed to be lightweight, reducing the amount of data exchanged during the communication process.

  6. Bi-directional Event-Driven Communication: WebSocket is particularly well-suited for real-time applications and scenarios where events on either the client or server side trigger the need for immediate communication.

  7. Cross-Origin Support: WebSocket supports cross-origin communication, allowing a web page from one domain to establish a connection to a server on another domain.

  8. Security: WebSocket connections can be secured using the same TLS/SSL protocols that are used to secure HTTP connections, providing a secure channel for data transmission.
WebSocket is commonly used in scenarios where real-time data updates, live streaming, online gaming, and other applications requiring low-latency communication are crucial. It has become an essential technology for building interactive and dynamic web applications. The protocol is standardized by the Internet Engineering Task Force (IETF) as RFC 6455.


Implementing ws Image

WebSocket is a powerful communication protocol that establishes a two-way communication channel between a client and a server.

Unlike the traditional HTTP protocol, commonly used for communication in browsers, WebSocket allows the server to send messages to the client without waiting for a client request. In the HTTP model, a client must initiate a request for communication.

Once a WebSocket connection is established between the client and the server, the WebSocket channel remains open in the background, minimizing overhead and latency compared to traditional HTTP connections. Similar to creating an HTTP server to handle requests, you can leverage NodeJS to build a WebSocket server.

In this tutorial, you'll learn how to create your WebSocket server using NodeJS. The complete implementation code can be found in this node-websocket-example repo.

Creating a WebSocket Server in NodeJS:

To create a WebSocket server in NodeJS, we'll use the popular ws module, specifically designed for WebSocket implementation.

 => Start by installing the ws module for your project:



With the module installed, create an index.js file to initialize a new WebSocket server. Import the ws module and open a WebSocket server connection on a designated port, such as port 8080:


Next, let's enhance the server code to handle potential message, errors and the closing of connections


Here’s the full NodeJS Server code for ws WebSocket connections, Create a file named index.js and add the following code:



Now, you have a basic WebSocket server. The server listens on port 8080, and the client connects to it. You can type messages in the client's input field and see the messages echoed back from the server. Feel free to expand and customize this example based on your requirements.