简体   繁体   English

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

[英]Detecting and pushing a stream of events to a web browser (HTML5, PHP, PostgreSQL)?

My currently in-development website is written in PHP. 我当前正在开发的网站是用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. 现在,虽然我确定使用EventSource和PHP文档来存储适当的数据:行会起作用,但我完全不知道当实际需要发送新消息时,如何通知该PHP文档。

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). 我的基本意思是,当发生操作时,将在PostgreSQL数据库中包含消息信息(例如已执行的操作)的条目。 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. 但是,让“消息传递” PHP文档的每个实例(与EventSource连接的实例)连续轮询PostgreSQL是否有新消息并没有效率。 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. 如果一次有50个用户处于活动状态,那将是50个实例轮询PostgreSQL,并且您可能会看到,这不是非常有效地使用资源。

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. 理想情况下,我希望能够调用一个函数,该函数指示已执行某项操作,然后将其发送给“消息传递” PHP文档的所有其他实例,以便他们可以解释消息并查看消息是否相关并将其推回给客户。

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. 本质上,我需要一种方法,通过在另一个PHP实例中调用带有消息信息的函数,以通知正在运行的PHP实例(通过Apache启动)正在创建新消息。 I don't need assistance with getting the messages to the client; 我不需要帮助将消息发送给客户。 I can do that with EventSource. 我可以使用EventSource做到这一点。

Does anyone have any suggestions as to how this task could be undertaken? 是否有人对如何执行此任务有任何建议?

解决该问题的常规方法是使用Java小程序(可以将套接字打开回原始服务器)或使用长轮询(例如Comet )。

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. 通过使用带有消息计数键值和消息$ i键值的memcache(其中$ i是递增数字),我已经成功地做到了这一点。 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. 一个PHP文档通过长时间轮询连接到该文档,并且它不断检查以查看message-$(messages-count)是否存在,在这种情况下将其返回。

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. 还有更多的事情,因为如果一次创建它们将返回多条消息,并且还可以将初始校验号($ i)加载为$ _GET参数,但这实际上是它的工作方式。 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). 它几乎是即时的,可以通过PHP将新消息轻松添加到内存缓存中(每次创建新消息时,都会增加messages-count)。

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

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