简体   繁体   English

如何在node.js和PHP之间进行通信?

[英]How to communicate between node.js and PHP?

I have an existing PHP website and I'm looking to add a real time notification system using node.js 我有一个现有的PHP网站,我正在寻找使用node.js添加实时通知系统

I have outlined the structure of my application into the diagram below : 我在下图中概述了我的应用程序的结构:

在此输入图像描述

I'm assuming communication between PHP and Node.js when the notification is generated is the only way I can achieve a realtime notification (unless I recode my entire website in node.js which is not possible) 我假设生成通知时PHP和Node.js之间的通信是我可以实现实时通知的唯一方法(除非我在node.js中重新编码我的整个网站,这是不可能的)

How do i communicate to node.js from PHP ? 我如何从PHP与node.js进行通信? (both exist on the same server or atleast the same local LAN) (两者都存在于同一台服务器上或至少存在于同一本地局域网中)

I have found a few solutions online and on a few SO threads : 我在网上和几个SO线程上找到了一些解决方案:

Are there other better ways of doing this ? 还有其他更好的方法吗?

Also all the notifications are not beamed to all subscribers. 此外,所有通知都不会发送给所有订阅者。

So, a notification generated by User 1 will only be beamed to User 2, 3 and 4 (because of a few business rules of my web app) This permission system needs to be maintained. 因此,用户1生成的通知将仅发送给用户2,3和4(由于我的Web应用程序的一些业务规则)。需要维护此权限系统。 How do I make sure that all the subscribers do not receive all notifications ? 如何确保所有订阅者都没有收到所有通知?

What is the most efficient way to achieve this ? 实现这一目标的最有效方法是什么?

I know this answer comes up really late but I've run across the same problem a couple of days ago. 我知道这个答案很晚才出现,但几天前我遇到了同样的问题。

Our solution to this would be using a message broker like RabbitMQ in the middle. 我们的解决方案是使用像RabbitMQ这样的消息代理。 Basically, PHP as the producer creates messages when there is something to be pushed, and Node.js, on the other side as a consumer listens to the queues and publishes messages whenever there is something new. 基本上,PHP作为producer在有东西被推送时创建消息,而Node.js作为consumer在另一侧监听队列并在有新内容时发布消息。

This method has a couple of advantages over a direct communication between PHP and Node.js. 与PHP和Node.js之间的直接通信相比,此方法有一些优点。

  • First of all, it is async. 首先,它是异步的。 Therefore, the user will perform its action and doesn't have to wait. 因此,用户将执行其操作而不必等待。
  • If, for some reason, Node.js fails, since messages will be stored in RabbitMQ, once you restart node it will simply continue from where it stopped. 如果由于某种原因,Node.js失败,因为消息将存储在RabbitMQ中,一旦重新启动节点,它将从它停止的地方继续。
  • You have Php and Node.js loosely coupled. 你有Php和Node.js松散耦合。
  • It works. 有用。

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

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