WebSockets in NodeJS Part 1

As there was a boom in technology, traditional HTTP was never enough for fulfilling the needs because they were too slow. In this age of live streams, remote work, and video conferencing so for a seamless experience an upgrade was required. In this tutorial, we will introduce you to WebSockets. We will go through some basics of Web sockets and we will be implementing it on a basic level. This tutorial has two parts. This is the first part of using Websockets in NodeJS.

What are Websockets?

Web Sockets are web-friendly, event-driven alternatives to HTTP. Web sockets do the same thing as HTTP but in a much better and faster way. When a WebSocket request is made to HTTP, it makes the traditional HTTP a WebSocket connection as well. This is done on the protocol level only which simply means the TCP connection does not change. It does not matter if it is working with WebSocket or HTTP.  

  1. For using WebSocket, a client has to send a request to the server for an upgrade.
  2. If the server supports WebSocket, the server will accept the request and then switch the HTTP protocol to WebSocket .
  3. After the switching, the server will turn the HTTP into WebSocket and will form a connection between the client and the server.
  4. Then the wait for an event starts. The WebSocket waits until the event happens. A function is attached to the event which launches when the event happens. For example, if we talk about a chat application then you do not have to place a request every time for the next message. As WebSockets are event-driven. WebSocket pushes a new incoming message from the client automatically to the server.
  5. a new incoming message from the client is automatically pushed to the server.

Websockets play a crucial role in places where we need real-time updates. Some of the examples of WebSocket applications are

  • Financial and sports updates.
  • Chat applications.
  • Social feeds.
  • Multi-player gaming.
  • Live streaming.
  • Location-based apps.
  • Collaborative work.

Websockets in NodeJS has played a huge role in enhancing the experience of the applications above. HTTPS is stateless but WebSockets maintain their states and they also utilize full-duplex connection. Other than this, Headers are only sent once when sending upgrade requests in the case of WebSockets. Let’s jump into more detail and move towards implementing it. The practical work is always more fun than the theory so let’s get right to it. 

Setting up the development environment

For implementing WebSockets we will need to set up our environment. You can follow the steps given below or if you are completely new to NodeJS you can visit our tutorial setting up NodeJS for the first time. This tutorial will help you get familiar with NodeJS. Now, let’s continue with our tutorial. For this tutorial first, we will need to install the required files and packages. First of all, create a new folder and open up the terminal in the folder. Run the following commands one by one by entering them in the terminal. 

  • npm init -y:
    • Initializes node and will create package.json file.
  • npm install websocket:
    • Installs the WebSocket library.
  • npm install ws:
    • Installs the ws library which is a WebSocket implementation. We will create chat application using this library
  • npm express:
    • installs express which we will use to create simple HTTP server. 

Once done with the installation, create a new file in the folder we created earlier and name it index.js. This index.js is the server-side JS file   Now for the client-side, we will create two files. We will name the first HTML file index.html and the second one index2.html.  

So this is it for the first part of WebSockets in NodeJS. In this part, we look at the basics of WebSocket and also set up the environment for implementing the WebSocket. In the second part, we will be implementing the web socket so stay tuned for the second part of the WebSocket in NodeJS. Also if you want to learn more about WebSockets you can always visit https://www.npmjs.com/package/websocket

Scroll to Top