我有一个现有的PHP应用程序,我需要添加实时通知。 为了实现这一点,我安装了node.js(打算为所有实时功能添加socket.io)</ p>

但是。 尽管花了最后三个小时研究并试图了解如何整合两者,但我发现自己没有接近理解。</ p>

我目前正在使用以下内容:< / p>

  • Apache </ li>
  • PHP </ li>
  • Nginx(作为Apache的反向代理,用于所有静态内容,如
    images / css文件等)</ li>
  • MySQL </ li>
    </ ul>

    我已编写代码,从客户端向PHP发送事件并插入通知 进入数据库。 当收件人刷新他们的页面时,他们当然会看到通知。 我只需要node.js来处理推送到客户端的实时时间,但是我不知道如何设置它。</ p>

    我真正需要知道的是,鉴于此 方案,如下所示:</ p>

    1. 如何/什么是node.js / websocket连接到客户端实例化,给定
      我希望所有内容仍然通过 Apache / PHP?</ li>
    2. 如何从PHP向Node.js发送消息并指示它将通知推送到客户端?</ li>
    3. 什么样的支持 为了支持这个,我需要对我的设置进行最终修改吗?</ li>
      </ ol>

      最终我希望能够运行PHP函数并期望节点。 js / socket.io / websockets将通知推送到客户端。 我根本不知道如何到达那里。</ p>

      提前感谢任何示例/信息/指南。 </ p>
      </ div>



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.

doze79040 你可以看一下
一年多之前 回复
dongli5785 1.当客户要求时;2.通过HTTP;3.设置nginx。
7 年多之前 回复


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.

duanhan3067 我该怎么办?我已经在.php扩展上创建了我的整个项目,现在当我到达我需要实时通知功能的部分时,它问我nodejs和socket io。我研究了它们,现在我想要结束 在PHP文件上加载我的客户端的<script>它甚至不显示那个php文件。
接近 5 年之前 回复
doutonghang2761 好吧那么@Brad我想我别无选择了:P我会在这里问问题。
接近 5 年之前 回复
doubo1711 虽然我会收取项目费用的咨询,但目前我的日程表中没有任何可用性。 此外,Stack Overflow并不是聘请顾问的合适场所。 我建议在网站上找一位专门聘请知识渊博的顾问的顾问。
接近 5 年之前 回复
duan5801 我会加你。
接近 5 年之前 回复
dongzhuang2030 您应该在Stack Overflow上发布一个新问题,并在评论中提供一个链接。
接近 5 年之前 回复
douzhi3105 我可以拥有你的脸书ID吗?我想跟你谈谈上面gordyr所描述的同样的问题。
接近 5 年之前 回复
dongyouzhui1969 当然,如果你以后没有计划任何增长。
接近 7 年之前 回复
duadpnld426905 但是,如果我们只使用一个节点服务器,这是可行的解决方案吗?
接近 7 年之前 回复
douchen7324 这当然是一个选项,但你有效地用pub / sub重新发明了这个轮子,然后你必须管理一个上/下的服务器列表,并处理你的PHP脚本将要挂起的事实 (除非你使用multicurl,这是一个真正的麻烦这样的事情)。
接近 7 年之前 回复
dqrm8199 而是使用Redis作为PHP和Node.js之间的通信通道,您如何看待使用Curl将请求从PHP发送到Node.js应用程序? 接下来,在Node.js应用程序中的请求事件中,我们将向连接的套接字发出数据/通知。
接近 7 年之前 回复
duanliusong6395 这听起来很完美,特别是因为我已经在使用Redis来构建活动流。 关于使用脚本标签的部分就像灯泡一样。 正是我所追求的。 非常感谢。
7 年多之前 回复

这个问题很老但我在尝试实现websockets时发现它可能会帮助其他需要纯PHP解决方案的人 可以集成到现有项目中而不会进行太多的黑客攻击。</ p>

Ratchet - PHP WebSockets < / a> </ p>

只有要求是PHP的zmq绑定,它用作将数据从PHP脚本传递到websocket服务器的方法。 如果您的PHP实例没有使用它构建,那么您可以在此处找到它</ p>

Github repo是这里,有很多例子可以帮助你实现这里 </ p>
</ div>



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

Csdn user default icon