Welcome folks today in this blog post we will be showing that how to handle concurrent request inside your node JS and Express server using the concept of worker Threads so we will be using this library in order to manage multiple users which are using your node JS and Express server all the source code of the example will be shown below
Get Started
so now to get started guys first of all we need to insert initialise a simple node JS project by following the below commands
npm init -y
so this will create the package.json file guys inside your root directory now we need to install the dependencies which will be needed for this project
sofa the simple project guys we will only need the express JS library in our to initialise our web server so install this library using the below command
npm i express
so now is we will be initialising a very simple express.js web server which will be listening on port 4000
index.js
1 2 3 4 5 6 7 8 9 |
const express = require('express') const app = express() const port = 4000 app.listen(port,() => { console.log("App is listening on port 4000") }) |
Now we need to add two get requests inside this express.js server to illustrate the situation of worker threads. Kindly add this source code in the index.js file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let counter = 0 app.get('/',(req,res) => { counter++; res.status(200).json({counter}) }) app.get('/heavy',(req,res) => { for(let i=0;i<100000000000000000;i++){ counter++; } res.status(200).json({counter}) }) |
as you can see that guys we have to request out there first of all we have a simple get request to the home route and here we have the counter variable which is equal to zero and inside this get request we are just returning the counter variable value and we’re just incrementing it by 1
and also we have a second route which is Slash heavy by the name itself this route will do the heavy lifting quote and in this route we are having a simple for loop which will run from 0 to very big number and again we’re doing the same concept here we are incrementing the value of Counter and then returning it as a JSON response
Problem With this Approach
so as you can see that guys whenever we are running this application and whenever we are opening routes in two different Windows then our application is hanging our express JS server is hanging because of that second route which is Slash heavy because it is unable to fulfill the concurrent request which are happening at the same time so this will reduce the effectiveness of the application as you can only serve one user at a time so now we need to find out a solution that we can handle concurrent users at the same time
As you can see it is blocking the main thread of the node.js application. So that other users accessing your app will face a very big problem in accessing your app until this route is completed it’s operation. guys this problem is recurring because we have a single thread in inside our Express application which is called as the main thread which is responsible for updating the user interface of the application and this thread is busy in the slash heavy route because it is doing the heavy lifting it is there inside the for loop and whenever this is hang you are in trouble so you need to find out a solution that you need to initialise this code to a separate thread so that a separate thread with will handle this route and your main thread will be free and it can handle the other users which are coming to the application
Using Worker Threads to Solve Problem
so now guys we will be using the worker Threads library which is a built-in module of node.js with dhoti to install it so we need to add the below code
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
const express = require('express') let {Worker} = require('worker_threads') const app = express() const port = 4000 let counter = 0 app.get('/',(req,res) => { counter++; res.status(200).json({counter}) }) app.get('/heavy',(req,res) => { const worker = new Worker('./worker.js') worker.on('message',(data) => { res.status(200).json({data}) }) }) app.listen(port,() => { console.log("App is listening on port 4000") }) |
As you can see that guys in the above code we are importing the worker Threads package and from that we need to import the worker method and then inside our slash heavy route we are initialising a new instance of this worker Constructor and here we are passing the part of the file to which we will be initialising this heavy lifting code so we have creating a new file which is called as worker.js so where we will write all this code. so as you can see that guys in the above code we are listening for that event which is called as message which will automatically Trigger whenever we send the data from the worker.js file and here we are using the on method of the worker object so we will get this data inside the callback function and simply we are returning this data as a JSON response to the client
so now we need to make a separate file ka is inside the root directory which will be called as worker.js and simply you need to copy paste the below code inside it
worker.js
1 2 3 4 5 6 7 8 |
const {parentPort} = require('worker_threads') let total = 0 for(let i=0;i<100000000000000000;i++){ total++; } parentPort.postMessage(total) |
so as you can see that guys we have copy pasted all the source code which is which was creating the problem so this contains the actual for look it will run for a long period of time we have a simple total variable and basically in each iteration we are incrementing the total variable value by one and then lastly we are using The post message method to to send this value to the express JS server and now we need to listen for this event guys which will occurred automatically whenever we call the postmessage method so now we need to add the code inside our express JS server.
so now if you execute this application guys in the browser you will not face any sort of problem so let’s suppose if you have 10 users coming to your application and let suppose one user has open the slash heavy route inside the browser then that route will not create the problem to the rest of the nine muses all the users can experience your application smoothly now so this is the advantage of using worker Threads inside your application this makes your application fast effective and also it allows you to manage concurrent users inside your node JS and Express application