简体   繁体   中英

Building Websites only on NodeJs and Express blocking requests over http

I have a question regarding the examples out there when using Nodejs, Express and Jade for templates.

All the examples show how to build some sort of a user administrative interface where you can add user profiles, delete them and manage them.

Those are considered beginner's guides to NodeJs. My question is around the fact that if I have have 10 users concurrently accessing the same interface and doing the same operations, surely NodeJs will block the requests for the other users as they are running on the same port.

So let's say I am pulling out a list of users which may be something like 10000. Yes I can do paging, but that is not the point. While I am getting the list from the server another 4 users want to access the application. They have to wait for my process to end. That is my question - how can one avoid that using NodeJS & Express?

I am on this issue for a couple of months! I currently have something in place that does the following:

  1. Run the main processing of stuff on a port
  2. Run a Socket.io process on a different port
  3. Use a sticky session
  4. The idea is that I do a request (like getting a list of items), and immediately respond with some request reference but without the requested items, thus releasing the port.
  5. In the background "asynchronously" I then do the process of getting the items. Upon which when completed, I do an http request from one node to the socket node port node SENDING the items through.
  6. When that is done I then perform a socket.io emit WITH the data and the initial request reference so that the correct user gets the message.
  7. On the client side I have an event listening for the socket which then completes the ajax request by populating the list.

I have SOME success in doing this! It actually works to a degree! I have an issue online which complicates matters due to ip addresses, and socket.io playing funny.

I also have multiple workers using clustering. I use it in the following manner:

  1. I create a master worker
  2. I spawn workers
  3. I take any connection request and pass it to the relevant worker.

I do that for the main node request as well as for the socket requests. Like I said I use 2 ports!

As you can see I have had a lot of work done on this and I am not getting a proper solution!

My question is this - have I gone all around the world 10 times only to have missed something simple? This sounds way to complicated to achieve a non-blocking nodejs only website.

I asked myself - surely all these tutorials would have not missed on something as important as this! But they did!

I have researched, read, and tested a lot of code - this is my very first time I ask anything on stackoverflow!

Thank you for any assistance.

PS One example of the same approach is this: I request a report using jasper, I pass parameters, and with the "delayed ajax response" approach as described above I simply release the port, and in the background a very intensive report is being generated (and this can be very intensive process as a lot of calculations are being performed)..! I really don't see a better approach - any help will be super appreciated!

Thank you for taking the time to read!

I'm sorry to say it, but yes, you have been going around the world 10 times only to have been missing something simple.

It's obvious that your previous knowledge/experience with webservers are from a blocking point of view, and if this was the case, your concerns had been valid.

Node.js is a framework focused around using a single thread to execute code, which means if it does any blocking operations, no one else would be able to get anything done.

There are some operations that can do this in node, like reading/writing to disk. However, most node operations will be asynchronous.

I believe you are familiar with the term, so I won't go into details. What asynchronous operations allows node to do, is to keep this single thread idle as much as possible. By idle I mean open for other work. If your code is fully asynchronous, then handling 4 concurrent users (or even 400) shouldn't be a problem, even for a single thread.

Now, in regards to your initial problem of ports: Once a request is received on a given port, node.js execute whatever code you have written for it, until it encounters an asynchronous operation as soon as that happens, it is available to to pick up more requests on the same port.

The second problem you inquire about, is the database operation. In this case, node-js would send the query to the database (which takes no time at all) and the database does that actual execution of the query. In the meantime, node is free to do whatever it wants, until the database is finished, and lets node know there is a result to fetch.

You can recognize async operations by their structure: my_function(..., ..., callback). Function that uses a callback function, is in most cases asynch.

So bottom line: Don't worry about the problems around blocking IO, as you will hardly encounter any in node. Use a single port if you want (By creating multiple child processes, you can even have multiple node instances on the same port).

Hope this explains it good enough. If you have any further questions, let me know :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM