An explanation of the nodejs style of development for a PHP developer in specific circumstances

I am a PHP web application developer who has created several large PHP projects with CodeIgniter. PHP has always done this job, but now I am working on a new project that I am creating using the client side javascript extjs4 infrastructure. And I have some questions for experienced nodejs developers.

In my last PHP project, a user login request required my server to call the API on Facebook. The way I dealt with this in order to improve scalability was that my client completed the initial login request, the server will send the request to the gearman job job server, and the background workflow will grab the job and make an API call. In the meantime, the server will respond to the client, and then the client browser will start polling the server using AJAX to see if the job is completed. (Oh, and I transferred the results of the Facebook API call from the worker to the memcached application server). I did this to free up application servers to handle more concurrent requests from users since blocking PHP, and the Facebook API call takes a few seconds.

My question is: is this whole model of application servers, a request server for working with a reducer and background workers really make sense for developing nodejs, since nodejs is not blocked? Can I just accept the ajax request from the client to log in, call the facebook API from the application server and wait for its response (when handling other user requests, since nodejs is not blocked), and then respond to the user?

I am also considering joining nodejs in order to use the excellent heroku environment.

+5
source share
1 answer

The short answer is yes, the way you usually deal with this on a node system is exactly the way you describe it.

node , . node-facebook-client ( npm, API- facebook)

  console.log('start');

  client.graphCall(path, params, method)(function(result) {
    // fires whenever request is completed
    console.log('facebook returned');
  });

  console.log('end');

  start
  end
  facebook returned

, , node ( ). , , node - . "" "facebook",

  console.log('start');

  client.graphCall(path, params, method)(function(result) {
    // fires whenever request is completed
    console.log('facebook returned');
    console.log('end');
  });

, , , node. child_process.fork:

var cp = require('child_process');

var n = cp.fork(__dirname + '/sub.js');

n.on('message', function(m) {
  console.log('PARENT got message:', m);
});

n.send({ hello: 'world' });

script, 'sub.js' :

 process.on('message', function(m) {
   console.log('CHILD got message:', m);
 });

 process.send({ foo: 'bar' });

send(), , .

+2

All Articles