Skip to content
Advertisement

How are Objects with Functions Handled in Node.js?

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) {
    this.link = URL;
    this.views = views;
    this.make_requests();
}

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

        let link = this.link;
        let views = this.views;

        for (let index = 1; index < views + 1; index++) {
            xhr.open("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!");
                    }
                }
            };

            xhr.send(null);
        }
    } catch (error) {
        console.log(error.message);
    }
}

}

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.

Advertisement

Answer

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement