检测事件流并将其推送到 Web 浏览器(HTML5、PHP、PostgreSQL)?

发布于 2024-10-17 07:39:11 字数 658 浏览 2 评论 0原文

我目前正在开发的网站是用 PHP 编写的。当用户使用该网站时,他们将执行操作,我希望能够将这些操作的通知推送给他们连接的其他用户。

现在,虽然我确信使用 EventSource 和 PHP 文档来提供适当的 data: 行会起作用,但我完全不知道当实际需要发送新消息时应该如何通知该 PHP 文档。

我本质上的意思是,当一个操作发生时,PostgreSQL 数据库中将会有一个条目,其中包含消息信息(例如所采取的操作)。然而,让“消息传递”PHP 文档的每个实例(EventSource 连接到的实例)连续轮询 PostgreSQL 来获取新消息的效率并不高。如果同时有 50 个用户处于活动状态,则将有 50 个实例轮询 PostgreSQL,正如您可能会看到的那样,这并不是一种非常有效的资源利用。

所以我想知道是否有人对可以帮助解决此问题的软件有任何建议。理想情况下,我希望能够调用一个函数来指示已采取的操作,然后将其发送给“消息传递”PHP 文档的所有其他实例,以便他们可以解释消息并查看是否相关并将其推回给客户。

本质上我需要一种方法来通知正在运行的 PHP 实例(通过 Apache 启动)正在创建新消息,方法是使用消息信息调用另一个 PHP 实例中的函数。我不需要帮助将消息发送给客户;我可以使用 EventSource 做到这一点。

有人对如何完成这项任务有任何建议吗?

My currently in-development website is written in PHP. As users are using the site, they'll be performing actions and I'd like to be able to push notifications of these actions to other users that they're connected to.

Now while I'm sure that using EventSource and a PHP document to server up the appropriate data: lines would work, I've got absolutely no idea how I should notify that PHP document when a new message actually needs to be sent.

What I essentially mean is that when an action takes place, there will be an entry into the PostgreSQL database with the message information (such as the action that was taken). However, it's not efficient to have each instance of the "messaging" PHP document (the one that EventSource is connected to) to continuously poll PostgreSQL for new messages. With 50 users active at once, that would be 50 instances polling PostgreSQL, and as you can probably see, not a very efficient use of resources.

So I'm wondering whether anyone has any suggestions as to software that might assist with this problem. Ideally I'd like to be able to call a function that indicates an action has been undertaken, which is then sent all the other instances of "messaging" PHP document so that they can interpret the message and see whether it's relevant and push it back to the client.

Essentially I need a way to notify running PHP instances (that were started via Apache) of a new message being created, by calling a function in another PHP instance with the message information. I don't need assistance with getting the messages to the client; I can do that with EventSource.

Does anyone have any suggestions as to how this task could be undertaken?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明月松间行 2024-10-24 07:39:11

解决该问题的传统方法是使用 java applet(它可以打开返回原始服务器的套接字)或使用长轮询(例如 comet)。

Conventional ways of solving the problem are using a java applet (which can open a socket back to the originating server) or using long polling (e.g. comet).

绻影浮沉 2024-10-24 07:39:11

我通过使用带有 messages-count 键值和 message-$i 键值的 memcache 成功地做到了这一点,其中 $i 是一个递增的数字。 PHP 文档通过长轮询连接,并不断检查 message-$(messages-count) 是否存在,如果存在则返回它。

这还有更多内容,因为如果一次创建多个消息,它会返回多个消息,并且还可以将初始检查编号 ($i) 作为 $_GET 参数加载,但这本质上就是它的工作原理。它几乎是即时的,新消息可以通过 PHP 轻松添加到内存缓存(每次创建新消息时,都会增加消息计数)。

I've succeeded in doing this by using memcache with a messages-count key-value and a message-$i key-value where $i is an incrementing number. A PHP document is connected to via long polling and it continuously checks to see whether message-$(messages-count) exists, in which case it returns it.

There's a bit more to this since it will return multiple messages if they're created at once and also can load the initial checking number ($i) as a $_GET parameter, but this is essentially how it works. It's near instant and new messages can easily be added to memcache via PHP (each time you create a new message, you increment messages-count).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文