Node.js Worker Threads Example to Speed Up Express.js Server & Manage Concurrent Requests in JS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign I will be showing you that how to handle multiple and concurrent requests inside your node.js application using the concept of worker threads so many a times guys whenever you deploy your application you get a lot of users to your node.js application or express application so you need to handle concurrent users so in that scenario the option of worker threads comes into the picture worker threads is not a third party Library it's a part of node.js ecosystem and you can now see if you just type here worker threads inside the node.js documentation it will give you all these methods that you will need so I will be showing you a very simple example on how to handle concurrent request and basically I will be showing you an example where this worker thread can be used so let me go into the node.js project guys so first of all you need to Simply initialize your node.js application just install Express server and PMI Express so this will install the express server on your machine so after this just start your application let me just start it node mod index.js so as you can see here let me write this code once again so first of all we will import our Express dependency so we will simply say const Express and we will require the express dependency so now to initialize this simple Express app guys we will app is equal to express so we will initialize the port number so let me just run this application on Port 4000 so after this guys we will simply use the listen method and we will pass this port number and in the Callback function we will simply say that app is listening on port number four thousand so now let me show you the two request guys so basically this Express server will be having two routes here this will be a get route so whenever the user goes to the home page we will be showing a simple message to the user we will have a simple Global variable right here at the very top which will be equal to zero counter is equal to zero so we will just pass this value we will simply increment it by 1 and then basically we will pass this value to the user we will put the status 200 and then inside our Json response we will get the counter like this this is a simple get request whenever you go to the home page we are incrementing this counter variable to 1 here and then we are simply returning this back to the client so if you just open this now localhost 4000 you will now see you will get a simple Json response here counter is equal to 1. if you again refresh it it will increase to two three four five six so now it is perfectly fine guys you can open this in each and every window you can open this inside multiple windows and your application will work smoothly in all the windows so concurrent users you can handle it nothing is there nothing no problem is there now let me show you the second route guys where we will be doing the heavy lifting so we will initialize the second route here basically this route will be slash heavy this will again be a get request and this time what we will be doing guys we will be running a very infinite Loop kind of a situation so let's suppose we will be running a let I is equal to 0 and we will do this I smaller than one zero zero zero zero zero so you can see it's a very high value and this will take a lot of time this process is a lot of it takes a lot of time and we will do the same process we will make counter plus plus and lastly we will be again be returning this value back to the client in a Json structure like this so we are essentially doing the same thing but it will take a lot of time because this is a for Loop and now you can see it is take it will take a lot of time because this is a very long value so in order to evaluate this it will take a long time so now if I see if I go to the Home Route it is perfectly running fine here but as soon as I open this route here which is localhost 4000 slash heavy as I open this now guys you will see it will hang you can now see it will hang so most importantly it will also block all the requests which will happen to the node.js application if I hope open the Home Route again this would be you can now see you can't go to this so your node.js application is hanged here so no error is there application is fine but this is blocking the main thread so that you can't access your application so if you again open it in the new window localhost 4000 once again you can't access your application so this is the main problem guys that a lot of node.js developers face you can't access your application with let's suppose you are doing a one heavy lifting here in this get route here so this makes your application unaccessible to all the other users which are using your application so in that scenario guys the concept of worker threads comes into the picture so worker threads is not a third party Library it's a built-in Library and if you want to read the blog post related to this video I have given the link in the description of this blog post so you can read more about this I have let me go just uh give you the so you can read the blog post let me give it in the live chat to read more about it because this is a very important concept people don't pay attention to it so we need to now resolve this problem guys how to handle concurrent users so we need to require worker we will write worker here and here we need to require it from this worker threads Library this is the worker threads Library which is a built-in Library built-in package of node.js so you don't need to install it from this we have simply uh extracting the worker class so now we need to move all this code guys inside a separate file so we will create another file called as worker.js so let me just remove this and start from scratch so we need to create a separate file you can call this anything I will call this worker.js so we need to delete this code here and simply paste this code here all the heavy lifting we will do inside this file so let me also declare the counter variable is equal to zero so for this guys we need to import the dependency again this will be equal to worker port sorry this is equal to parent Port we need to parent Port class we need to again require it from the same module which is worker threads so this contains a class guys which is called as parent port and with the help of this class guys we will simply use parent Port dot post message method and simply we will need to pass the value of counters which we need to display so we will pass this value to this function counter like this so this will basically pass this value guys to the main thread guys inside index.js so you can see we are saying this worker now we need to initialize this make create a new object here so we will simply say let worker new worker and here we will pass the path of the file so we have created this file guys worker.js in the root directory so we are simply passing the path of the file right here to this Constructor and this contains a method guys which is on so from you can use this method guys in order to listen for all the events which which can occur error event is the exit is their message is their message error is there and online is there so we will use this message even guys so whenever we are sending this message using this post message this event is triggered inside this file we are catching it using this method using this event called as message so in the Callback function guys we will get the data in this case the data is actually the counter variable that we are passing here counter variable so this data will be contained inside this so basically if I console log the data so this will be a simple arrow function and lastly we just need to move this line in this directory so we will just pass this to the user to the client as a Json data like this counter so this needs to be data not counter because data is received here so you can see worker.js we have moved all this code to a different file and from this file we are passing this data to the main thread here main UI thread and here we are catching it using the message event and then we are returning back to the client so now if you just open this application once again guys you can see currently it is working here now if I open the slash heavy route guys once again it will take a lot of time here you can see it is again hanging here but this time you can see that I can easily access all the other routes of the application if I again open it in a separate window so there will be no hanging in other tabs so your the users other users that are using your application can easily navigate to all the routes so this route will take a lot of time but the other routes other users can use your application very smoothly so this is the solution guys worker threads offers you in order to handle multiple concurrent users at the same time if you have a very heavy lifting that you need to do inside one route you can move that code into a different file worker.js and then you can see you can even create multiple workers guys let's suppose we have a second heavy request like this let's suppose we can have heavy two and we are doing the same thing once again we can create a another file here this we can call this as worker 2 we can create another file here which is called as worker 2.js and inside this file once again we can paste the same code here worker 2.js inside this also let me take this now we are again incrementing it uh we are incrementing it by two or this time we are simply printing the message on the screen so which will be hello world so we can call this as empty so we are just printing out this message plus is equal to we are appending it like this and then we are passing this to the main thread so now we can simply say here inside index.js we are initializing another thread here like this and then we can pass the data so now you can see we are two work we have two worker threads out there working simultaneously this is the first one this is the second one and rest of the application with will run smoothly let's suppose we have another get request here which is some uh hello I have a get route here so this will not be affected by the these two routes here which is slash heavy and slash every two you can simply send out a message uh hello world like this so now if you see that guys if you go to localhost now you can see that if I open here slash heavy so it will take a lot lot of time here if I now open localhost slash heavy two this is another heavy route which is taking a lot of time and let's suppose if I open the next route which was Slash hello so you can now see it will return a simple message to the screen hello world so for requests are happening simultaneously guys without any problem guys you can now see uh some problem has occurred invalid string length uh let me just change this to the same logic that we defined earlier we are doing the same thing so just refresh it this application so this is how we do we are again doing the same thing heavy one and you can now see no problem is there you can simultaneously test out multiple routes here so multiple people can access your application without any delay so if you have any sort of long operation guys you can create a worker thread and basically you can assign that role toward this worker thread so this will not having any sort of effect to your overall application so you if you have a lot of work to be done you can simply move this to a separate Javascript file with with the help of worker threads simply create a new instance instantiate a new object of worker class and simply assign this to a separate worker so this will make sure that your overall application is not affected by that simple route so this was the concept guys of worker threats inside node shares to speed up your application or effectively handle multiple concurrent users at the same time so I have shown you the complete example you can also read my full blog post I have explained in theory as well alongside with complete source code examples so if you enjoyed this video guys please hit that like button subscribe the channel as well and I will be seeing you in the next tutorial until then thank you very much
Info
Channel: Coding Shiksha
Views: 8,486
Rating: undefined out of 5
Keywords: coding shiksha, worker threads, node worker threads, speed up node.js app
Id: OGkOxkSYrUg
Channel Id: undefined
Length: 15min 24sec (924 seconds)
Published: Tue Oct 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.