如何在PHP中实现服务器发送事件?

时间:2022-09-02 09:12:02

I had set up a server sent event script with php and a while loop, I did not want for the script to have to keep closing and have to repoll so I put it all in a while loop.

我已经设置了一个带有php和while循环的服务器发送事件脚本,我不希望脚本必须保持关闭并且必须重新播放所以我把它全部放在while循环中。

The issue was that the script was getting stuck and I had to abandon that route and I went with a node.js websocket backend instead.

问题是脚本卡住了,我不得不放弃那条路线,而是选择了node.js websocket后端。

My question is, if I ever went back to making a server sent event php script, how do I implement it?
while loops do not seem to cut it as it hangs the script, and if it is just connecting and disconnecting every second, it is no different than long polling, so how do I create a PHP script that will not hang, while also sending over the SSE messages?

我的问题是,如果我回到制作服务器发送事件PHP脚本,我该如何实现它? while循环似乎不会因为它挂起脚本而削减它,如果它只是连接和断开每一秒,它与长轮询没有什么不同,所以如何创建一个不会挂起的PHP脚本,同时也发送SSE消息?

1 个解决方案

#1


5  

You seemed to have issue on php output buffering. Try adding these line to the end of your while loop:

你好像在php输出缓冲上有问题。尝试将这些行添加到while循环的末尾:

ob_flush();
flush();

This should disable the output buffering.

这应该禁用输出缓冲。

EDIT You can also terminates the script after some time (i.e. 10mins) to reduce server load.

编辑您也可以在一段时间(即10分钟)后终止脚本以减少服务器负载。

I've created a library for you to do it very easily. Check it here.

我已经为您创建了一个非常容易实现的库。在这里查看。

Second Edit Do you have a reverse proxy such as nginx or varnish? This may be the reason because the proxy tries to cache the content of the output but the SSE script never ends until you stop it so the whole thing hangs. Other things that captures the output may have similar results such as mod_deflate.

第二个编辑你有一个反向代理,如nginx或清漆?这可能是原因,因为代理尝试缓存输出的内容,但SSE脚本永远不会结束,直到你停止它,所以整个事情都会挂起。捕获输出的其他内容可能具有类似的结果,例如mod_deflate。

Third edit If you have a reverse proxy, you can try to turn off caching to allow SSE to work.

第三次编辑如果您有反向代理,可以尝试关闭缓存以允许SSE工作。

There are another ways in PHP to disable output buffering. See the code below:

PHP中还有另一种禁用输出缓冲的方法。请参阅以下代码:

<?php
for($i=0;$i<ob_get_level();$i++){
    ob_end_flush();
}
@apache_setenv('no-gzip',1);
@ini_set('implict_flush',1);
ob_implict_flush(true);

#1


5  

You seemed to have issue on php output buffering. Try adding these line to the end of your while loop:

你好像在php输出缓冲上有问题。尝试将这些行添加到while循环的末尾:

ob_flush();
flush();

This should disable the output buffering.

这应该禁用输出缓冲。

EDIT You can also terminates the script after some time (i.e. 10mins) to reduce server load.

编辑您也可以在一段时间(即10分钟)后终止脚本以减少服务器负载。

I've created a library for you to do it very easily. Check it here.

我已经为您创建了一个非常容易实现的库。在这里查看。

Second Edit Do you have a reverse proxy such as nginx or varnish? This may be the reason because the proxy tries to cache the content of the output but the SSE script never ends until you stop it so the whole thing hangs. Other things that captures the output may have similar results such as mod_deflate.

第二个编辑你有一个反向代理,如nginx或清漆?这可能是原因,因为代理尝试缓存输出的内容,但SSE脚本永远不会结束,直到你停止它,所以整个事情都会挂起。捕获输出的其他内容可能具有类似的结果,例如mod_deflate。

Third edit If you have a reverse proxy, you can try to turn off caching to allow SSE to work.

第三次编辑如果您有反向代理,可以尝试关闭缓存以允许SSE工作。

There are another ways in PHP to disable output buffering. See the code below:

PHP中还有另一种禁用输出缓冲的方法。请参阅以下代码:

<?php
for($i=0;$i<ob_get_level();$i++){
    ob_end_flush();
}
@apache_setenv('no-gzip',1);
@ini_set('implict_flush',1);
ob_implict_flush(true);