Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to communicate between node.js and PHP?

I have an existing PHP website and I'm looking to add a real time notification system using node.js

I have outlined the structure of my application into the diagram below :

enter image description here

I'm assuming communication between PHP and Node.js when the notification is generated is the only way I can achieve a realtime notification (unless I recode my entire website in node.js which is not possible)

How do i communicate to node.js from PHP ? (both exist on the same server or atleast the same local LAN)

I have found a few solutions online and on a few SO threads :

  • Using redis pubsub : http://xmeng.wordpress.com/2011/11/14/pubsub-in-redis-using-php/
  • Using DNode : http://bergie.iki.fi/blog/dnode-make_php_and_node-js_talk_to_each_other/

Are there other better ways of doing this ?

Also all the notifications are not beamed to all subscribers.

So, a notification generated by User 1 will only be beamed to User 2, 3 and 4 (because of a few business rules of my web app) This permission system needs to be maintained. How do I make sure that all the subscribers do not receive all notifications ?

What is the most efficient way to achieve this ?

like image 570
YD8877 Avatar asked Oct 31 '22 21:10

YD8877


1 Answers

I know this answer comes up really late but I've run across the same problem a couple of days ago.

Our solution to this would be using a message broker like RabbitMQ in the middle. Basically, PHP as the producer creates messages when there is something to be pushed, and Node.js, on the other side as a consumer listens to the queues and publishes messages whenever there is something new.

This method has a couple of advantages over a direct communication between PHP and Node.js.

  • First of all, it is async. Therefore, the user will perform its action and doesn't have to wait.
  • If, for some reason, Node.js fails, since messages will be stored in RabbitMQ, once you restart node it will simply continue from where it stopped.
  • You have Php and Node.js loosely coupled.
  • It works.
like image 118
Savas Vedova Avatar answered Nov 09 '22 10:11

Savas Vedova