Threading in node.js with the cluster module

JavaScript has a reputation of being a single threaded language, for the most part it is if I only take into consideration core javaScript by itself. However when taking into account the full breath of what there is to work with in client side javaScript, as well as a node.js environment, it would appear that javaScripts reputation of being single threaded language is wrong, or at best a half truth.

In client side javaScript there is of course webworker which to some extent can be used to achieve threading in client side javaScript. Yes there are some limitations as doing anything with the DOM can only be done in the main thread, not the webworker context. Still this web worker constructor can be used to spin up a whole other event loop as a separate process on the guest operating system, and messages can be passed to and from it from the main thread of a page. What happens in the web worker context will not end up holding up the main thread of the page then as it is running in a whole other event loop, with a whole other thread.

However this post is about the cluster module in a node.js environment, which is one of several options when it comes to starting up more than one event loop when it comes to working wit sever side javaScript.

Read More

Client side javascript in a node.js environment using jsdom

There comes a time now and then that I need to work with html in a server side, node.js environment. I have written about a very helpful project called cheerio that works well if I just want to grab at something like a link, or maybe make some kind of edit to html. However cheerio is not an actual emulation of a browser environment. There are other projects that aim to actually emulate an actual usable browser environment for the purpose of getting client side apparitions to work in a node.js project server side. The npm package jsdom is one such project, and as such this post will be about how to use jsdom to bring a browser environment to node.

Read More