简体   繁体   English

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

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

I have an existing PHP application, to which I need to add realtime notifications. 我有一个现有的PHP应用程序,我需要添加实时通知。 In order to achieve this I have installed node.js (intending to add socket.io for all real time functionality) 为了实现这一点,我安装了node.js(打算为所有实时功能添加socket.io)

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 PHP
  • Nginx (as a reverse proxy to Apache for all static content like images/css files etc) Nginx(作为Apache的反向代理,用于所有静态内容,如图像/ css文件等)
  • MySQL MySQL的

I have code already written which sends an event from the client to PHP and inserts a notification into the database. 我已经编写了代码,它将一个事件从客户端发送到PHP并将通知插入到数据库中。 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. 我只需要node.js来处理推送到客户端的实时时间,但我不知道如何设置它。

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? 如果/我希望所有内容仍然通过Apache / PHP提供,那么node.js / websocket连接到客户端的实例/时间是多少?
  2. How can I send a message from PHP to Node.js and instruct it to push the notification to the client? 如何从PHP向Node.js发送消息并指示它将通知推送到客户端?
  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. 最后,我希望能够运行PHP函数并期望node.js / socket.io / websockets将通知推送到客户端。 I just have no idea how to get there. 我根本不知道如何到达那里。

Thanks in advance for any examples/information/guides. 提前感谢任何示例/信息/指南。

What I would do in this scenario is set up a Node.js server with Socket.IO . 我在这种情况下要做的是用Socket.IO建立一个Node.js服务器。 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. 当客户端加载PHP页面时,您将有一个<script>标记指向您的Node.js服务器以加载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. 加载后,Socket.IO JavaScript客户端将连接到Node.js Socket.IO服务器并等待发出事件。

Now, since you want these events to be sent from PHP, you need a communication channel between your PHP application and Node.js. 现在,既然您希望从PHP发送这些事件,则需要PHP应用程序和Node.js之间的通信通道。 I recommend using Redis pub/sub for this. 我建议使用Redis pub / sub Basically, your PHP application publishes a message, and your Node.js servers that have subscribed to it will receive it. 基本上,您的PHP应用程序发布消息,并且已订阅它的Node.js服务器将接收它。 Those servers can then immediately pass a message on to the client to go get more data from PHP. 然后,这些服务器可以立即将消息传递给客户端,以便从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.) (我想你会发现,让你的Node.js服务器首先发送那些数据可能同样容易。)

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 . 如果需要,您可以将Node.js放在Nginx服务器后面, 但是您需要最新和最好的版本来支持真正的Web插槽

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. 这个问题很老,但是当我尝试实现websockets时我发现了这个问题,所以这可能会帮助那些需要纯PHP解决方案的其他人,这些解决方案可以集成到现有项目中而不需要太多的黑客攻击。

Ratchet - PHP WebSockets 棘轮 - 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. 只有要求是PHP的zmq绑定,它用作将数据从PHP脚本传递到websocket服务器的方法。 If your PHP instance isn't built with it then you can find it here 如果你的PHP实例不是用它构建的,那么你可以在这里找到它

Github repo is here , plenty of examples to get you going are here Github上回购是在这里 ,大量的实例,让你去是这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM