douyan8961 2013-05-09 23:12
浏览 64
已采纳

将Node.js(用于实时通知)添加到现有PHP应用程序

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.

  • 写回答

2条回答 默认 最新

  • doucheng7234 2013-05-09 23:18
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀