How are Objects with Functions Handled in Node.js?

Tags: , ,

I am currently using Node.js to handle the back-end of my website but I am unsure of how Websockets/Objects are handled together.

This is a template I am using as an example of my main class. (Sends web-requests to a specific page)

class ViewClass {
constructor(URL, views) { = URL;
    this.views = views;

make_requests() {
    try {
        const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
        const xhr = new XMLHttpRequest();

        let link =;
        let views = this.views;

        for (let index = 1; index < views + 1; index++) {
  "GET", link, false);

            xhr.onload = function (e) {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        console.log("View: " + index + " Sent Successfully!");
                    } else {
                        console.error("View: " + index + " Failed!");

    } catch (error) {


This is my Main Websocket File (Stripped for simplicity)

server.on('connection', function (socket) {
  console.log("Welcomed Connection from: " + socket.remoteAddress);

  socket.on('close', function (resp) {
    console.log(`[${GetDate(3)}] Bye!`);

  socket.on('data', function (buf) {
    // Take Views/URL from Front-end.
    // Initialise a new Object from ViewClass and let it run until finished.

Lets say I receive data from the WebSocket and that data creates a new ViewClass object and starts running immediately. Will that Now Running code block the input/output of the Node.js Server? Or will it be handled in the background?

If there is any information I can provide to make it clearer let me know as I am extremely new to Websocket/Js and I am more than likely missing information.


Your ViewClass code is launching views XMLHttpRequests and then doing nothing, but waiting for responses to come back. Because a regular XMLHttpRequest is asynchronous (if you don’t pass false for the async flag), the server is free to do other things while the code is waiting for the XMLHttpRequest responses.

Will that Now Running code block the input/output of the Node.js Server?

No, because this is asynchronous code, it will not block the input/output of the server.

Or will it be handled in the background?

Responses themselves are not handled in the background. Nodejs runs your Javascript in a single thread (assuming there are no WorkerThreads being used which are not being used here). But, waiting for a networking response is asynchronous and is handled by native code in the event loop in the background. So, while your code is doing nothing but waiting for an event to occur, nodejs and your server is free to respond to other incoming events (such as other incoming requests).

Emergency Edit:

This code:"GET", link, false);

Is attempting a SYNCHRONOUS XMLHttpRequest. That’s a horrible thing to do in a node.js server. That WILL block all other activity. Change the false to true to allow the xhr request to be asynchronous.

Source: stackoverflow