Using node.js and socket.io with a PHP application

I have a PHP application running. This allows the user to create private projects and invite others to it. Now, using node.js and socket.io, I want to make real-time comments, posts, etc.

What is the best architecture?

Now I see two solutions.

First:

  • The user sends an AJAX request to the PHP server: http://example.com/comment_add.php?text= ...
  • comment_add.php adds a comment to the database and notifies node.js through AMPQ (or is it better?), which passes comments to the channel’s subscribers.

Second:

  • The user sends an AJAX request to the node.js server: http://example.com//000/comment_add
  • Node.js sends a request to the PHP server (but how? What about authorization?), Receives a response, and then broadcasts the channels for channel subscribers.

What is the best way? Are there any other methods? How to implement this correctly?

+4
source share
4 answers

Finally, my working solution is No. 1.

When a user establishes a connection with node.js / socket.io, he simply sends a “subscribe” message to node.js with his PHP session identifier. node.js checks authorization using a POST request on a PHP server, and if everything is ok, the user can establish a connection.

Frontend sends all requests to PHP, as it was before node.js. PHP modifies some object, checks who can access the modified object, and sends a message (via AMQP or redis pub / sub, etc.) to node.js:

{ id_object: 125342, users: [5, 23, 9882] } 

node.js then check which of the listed users has active sockets and for each user sends a GET request to PHP:

 { userId: 5, id_object: 125342 } 

A special PHP controller that receives this request starts a request to get an object with the rights of this user ID and then sends a message to node.js with the resulting response. node.js, then through socket sends a response to the external user interface.

+1
source

When you decide to use node.js + socket.io to create a real-time web application, you no longer need to think about PHP and forget Ajax as well ... Socket.io will be the connection between the client and server.

But yes, you can use Ajax and PHP to quickly create sites and some other functions that are not needed in real time.

+4
source

The second way is the best method. You can use http to communicate with PHP from node.js. Authorization can be done in node.js, but each time passing auth credentials to PHP

+2
source

I came across the same issue a year ago when I started my last project at the university. I realized that my project is much better suited for using Node as a standalone. Node is very good at working with I / O, it can be anything from HTTP requests to a database request. Adding to Node-based PHP web server will add unnecessary complexity. If your application needs to perform intensive tasks with the CPU, you can quite easily create a "child" Node handler that performs the necessary operation and return the result to the parent node.

However, of these two methods, you mentioned that I would choose # 2. node.js can interact with your PHP server in several ways, you can look at creating a unix socket connection between your PHP server and node. If this is not possible, you can simply exchange data between Node and your PHP end using HTTP. :)

Take a look here, here is a solution to a question very similar to yours: http://forum.kohanaframework.org/discussion/comment/57607

0
source

Source: https://habr.com/ru/post/1411166/


All Articles