20

I have an existing PHP application, to which I need to add realtime notifications. In order to achieve this I have installed node.js (intending to add socket.io for all real time functionality)

However. Despite spending the last three hours researching and trying to get my head around how to integrate the two, I have found myself no closer to gaining an understanding.

I am currently using the following:

  • Apache
  • PHP
  • Nginx (as a reverse proxy to Apache for all static content like images/css files etc)
  • MySQL

I have code already written which sends an event from the client to PHP and inserts a notification into the database. When the recipient refreshes their page they of course see the notification. I simply need node.js to handle the real time pushing to the client but am at a loss as to how to go about setting it up.

What I really need to know, given this scenario, is the following:

  1. How/when is the node.js/websocket connection to the client instantiated, given that I wish all content to still be served via Apache/PHP?
  2. How can I send a message from PHP to Node.js and instruct it to push the notification to the client?
  3. What kind of back end modifications do I need to make to my setup in order to support this?

Ultimately I would like to simply be able to run a PHP function and expect node.js/socket.io/websockets to push the notification to the client. I just have no idea how to get there.

Thanks in advance for any examples/information/guides.

gordyr
  • 6,078
  • 14
  • 65
  • 123
  • 1. When the client requests it; 2. [Through HTTP](http://stackoverflow.com/questions/10048978/sending-messages-from-php-to-node-js); 3. [Set up nginx](http://stackoverflow.com/questions/5009324/node-js-nginx-and-now). – Waleed Khan May 09 '13 at 23:19
  • You might check [this](https://developer.hyvor.com/php/chat-app-with-php-nodejs) out – Supun Kavinda Apr 19 '19 at 08:05
  • @Supun Kavinda - Your link is helpful for a general example. the issue is that it applies to every connected user to the php file. how to send the request to different users, not all? say, have more than one conversation going on. I am able to keep track of users/conversations on php, but how to send the push notification to the proper connected user on the websocket? in the example, node client ID's are just integers, make them the same as the $_SESSION variable? how? – Zackattack Apr 11 '21 at 11:05
  • @Zackattack You may use "channels". The easiest way is go through [Pusher](https://pusher.com/)'s API docs and see how their channels work. You can implement something like that in your WS server. (It's pretty simple and you can do authentication via tokens or sessions). Then, you can subscribe to channels from the front-end. However, we are no longer using Node.js: we wrote our WS server in PHP thanks to the awesome [Ratchet package](http://socketo.me/). – Supun Kavinda Apr 11 '21 at 13:38
  • thanks for your reply, i was going to use ratchet originally, but it seems more complicated than node js. i spent hours trying to implement it and didnt get very far. I was able to get the node js working in under 30m, having no experience with node js prior – Zackattack Apr 11 '21 at 13:59

2 Answers2

17

What I would do in this scenario is set up a Node.js server with Socket.IO. This gives you a cross-browser method for sending near-real-time data to clients.

When the client loads your PHP page, you will have a <script> tag pointing at your Node.js server to load Socket.IO. Once loaded, the Socket.IO JavaScript client will connect to your Node.js Socket.IO server and wait for events to be emitted.

Now, since you want these events to be sent from PHP, you need a communication channel between your PHP application and Node.js. I recommend using Redis pub/sub for this. Basically, your PHP application publishes a message, and your Node.js servers that have subscribed to it will receive it. Those servers can then immediately pass a message on to the client to go get more data from PHP. (I think you will find though that it might be just as easy to have your Node.js server just send that data in the first place.)

You can put Node.js behind your Nginx server if you want, but you need the latest and greatest version for true web socket support.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 1
    This sounds perfect, especially since I am already using Redis to build activity streams. The part about using the script tag was like a lightbulb going off. Exactly what I was after. Many thanks. – gordyr May 09 '13 at 23:22
  • Instead using Redis as communication channel between PHP and Node.js , what do you think about sending request from PHP to the Node.js app with Curl? Next , on the request event in the Node.js app we will emit data/notification to the connected sockets. – user345602 Jan 19 '14 at 00:48
  • 2
    @user346665 That's certainly an option, but you're effectively re-inventing the wheel with pub/sub, and then you would have to manage a list of servers that are up/down, and deal with the fact that your PHP script is going to hang (unless you use multicurl which is a real hassle for something like this). – Brad Jan 19 '14 at 00:50
  • But if we are using only one node server , is this ok solution ? – user345602 Jan 19 '14 at 00:56
  • @user346665 Sure, if you aren't planning for any growth later on. – Brad Jan 19 '14 at 01:03
  • @Brad can i have your facebook id?I want to have conversation with you for same problem described above by gordyr. – SOuřaan Gřg Dec 22 '15 at 18:49
  • @SOuřaanGřg You should post a new question here on Stack Overflow and provide a link here in the comments. – Brad Dec 23 '15 at 01:26
  • @Brad I would have done as you said but I can't ask question randomly here.I mean here I must wait `90min` after posting one question.Any way I have a big project concern to social site which I can't even handle without an expert advice.Its getting even harder to ask question now.I want to take your advice personally.You can mail your fb id on :- sanjitgrg061@yahoo.com I will add you. – SOuřaan Gřg Dec 23 '15 at 04:29
  • 1
    @SOuřaanGřg While I do consult on projects for a fee, I do not have any availability in my schedule at the moment. Besides, Stack Overflow is not really the appropriate place to hire a consultant. I recommend finding a consultant on a site specifically for hiring knowledgeable consultants. – Brad Dec 23 '15 at 04:32
  • ok then @Brad I guess I have no option left :P I will be asking question here then. – SOuřaan Gřg Dec 23 '15 at 04:45
  • @Brad what should I do?I have created my whole project on .php extension and now when I reached to a part where I need to make real time notification feature it asked me `nodejs` and `socket io`.I studied them and now at end when I want to load ` – SOuřaan Gřg Dec 23 '15 at 04:57
0

This question is old but I found it when I was trying to implement websockets so maybe this will help someone else who needs a pure PHP solution which can be integrated into an existing project without too much hacking around.

Ratchet - PHP WebSockets

Only requirement is the zmq binding for PHP, which is used as the method to pass data from your PHP scripts to your websocket server. If your PHP instance isn't built with it then you can find it here

Github repo is here, plenty of examples to get you going are here

miknik
  • 5,748
  • 1
  • 10
  • 26