User Created Image
User Created Image

Reputation: 1182

Using node.js and socket.io with PHP application

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

What is the best architecture?

I see two solutions now.

The first is:

The second is:

What is the best way? Is there another methods? How to implement this properly?

Upvotes: 4

Views: 4902

Answers (4)

User Created Image
User Created Image

Reputation: 1182

Finally my working solution is #1.

When user establishing connection to node.js/socket.io he just send 'subscribe' message to node.js with his PHP session id. Node.js checks authorization using POST request to PHP backend and if all is OK allows user to establish connection.

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

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

node.js then check who from listed users have active sockets and for each user sends GET request to PHP:

{
    userId: 5,
    id_object: 125342
}

Special PHP controller receiving this request runs query to get object with rights of given user id and then sends message to node.js with resulting answer. Node.js then via socket sends answer to user's frontend.

Upvotes: 1

Chung Xa
Chung Xa

Reputation: 111

When you decided to use node.js + socket.io to make a real-time web-app, you don't need to think about PHP anymore and forget Ajax also... Socket.io will be the communication between client and server.

But yes, you can use Ajax and PHP for building websites fast, and some other functions that don't need real-time

Upvotes: 4

Jack
Jack

Reputation: 15872

I faced this same question a year ago when starting my final year project at University. I realized that my project was much better suited to using Node as a standalone. Node is very good at dealing with I/O, this can be anything from a HTTP requests to a database query. Adding in a PHP based Web Server behind Node is going to add un-needed complexity. If your application needs to perform CPU intensive tasks you can quite easilly spawn 'child' node processed which perform the needed operation, and return the result to your parent node.

However out of the two you methods you have mentioned I would choose #2. Node.js can communicate with your PHP server in a number of ways, you could look at creating a unix socket connection between your PHP server and Node. If that is unavailable you could simply communicate between Node and your PHP back end using HTTP. :)

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

Upvotes: 0

Pavan Kumar Sunkara
Pavan Kumar Sunkara

Reputation: 3025

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 passing auth credentials everytime to PHP

Upvotes: 2

Related Questions