Social network with PHP and Nodejs

To Do

The thing i am trying to achieve is a hybrid server to run and handle the needs of my Social Network website.

Build

I have created all of it on PHP and database is MySql, for real time updates i have used AJAX long polling which checks for new messages in every 'x' seconds. But i don't want time gap between rechecks and that's why i decided to use short polling. Short polling is achieved through Node.js+Socket.io.

Problems

  1. After the "NodeApp" is built, i am going to combine it on my website which is using apache. How can i integrate both of them together? For example : Pages are :

    • example.com/home.php #Apache
    • example.com/profile.php #Apache
    • example.com/messages #Node.js
  2. How can i load my php page as is and messages in between which is written in node.js for real time updates just like FB.(using websockets and then fall back to whatever is supported, this is done using [socket.io]) A small overview : http://postimg.org/image/p3vq4tkvr/

Things i know

  1. I can use http-proxy in node to use same URL but with different ports.
  2. I can use two dedicated ip with VPN, one for apache and one for Node.js.
  3. I don't want to use iFrames in between.
  4. I don't want to use different ports like www.example.com:3000 for node and www.example.com for apache.

Doing right now is about the same. I'll share my experience with you. It may come in handy. We was ready social network engine to phpFox. Decided to "spice up" section of personal messages using Node.js (used as a transport socket.io).

From the beginning, there was a problem with the connection PHP (phpFox) with Node.js. The bridge was found - dNode. This module allows you to use TCP-protocol to send data between virtual servers (drivers dNode there for many popular programming languages.)

Next problem - the database. Initially, we used MySQL with a list of tables (for mail):

mail | mail_text | user | user_info

Some tests have shown that use MySQL with node.js is not rational. For this reason it was decided to use for the "Dialogues" (the code name for personal messages written on node.js) non-relational database MongoDB. The script was written by Python (can be used and PHP, but for such tasks, I prefer to use Python - the best performance and speed of execution of scripts) that "peregenal" MySQL database to MongoDB, created such a scheme:

messages | dialogs | texts

The collection of unique dialogs are stored messages:

dialog_id | owner_user_id | viewer_user_id | last_message | some_info

And then everything is clear:

  1. When you open the page with the help of http://site.com/mail/ dNode, PHP passes the user_id on the side node.js.
  2. When node.js got user_id - he pulls out all the "dialogue" with the collection of "dialogs" and draws the main page.
  3. When a user opens a dialogue with the user, for example, John, a page opens with their correspondence http://site.com/mail/{John user_id}.
  4. With socket.io is real-time conversations.

That's it. I hope my experience will help you with something :)

Good luck!