Adding Node.js (for real-time notifications) to an existing PHP application

前端 未结 2 554
抹茶落季
抹茶落季 2021-01-30 11:55

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 ti

相关标签:
2条回答
  • 2021-01-30 12:10

    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

    0 讨论(0)
  • 2021-01-30 12:31

    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.

    0 讨论(0)
提交回复
热议问题