关于php:从Predis订阅功能中触发Laravel事件

Firing a Laravel event from within the Predis subscribe function

我有一个Laravel php应用程序和一个NodeJS客户端。客户端中发生了一些事情,我想更新我的php后端,因此我将消息发布到Redis频道,在Laravel应用中,我有一个Redis订阅者在该频道上侦听消息。

理想情况下,我想在收到消息时触发一个事件,但出现此错误

[Predis \\
esponse \\\\ ServerException]
在此上下文中仅允许ERR(P)SUBSCRIBE /(P)UNSUBSCRIBE / QUIT

我可以从订阅者内部做我需要做的事情,例如更新存储库等...但是我无法触发Laravel事件,这就是在这里要做的事情。


您需要对pubsub使用两个连接。订户连接不能发出subscribepsubscribeunsubscribepunsubscribe以外的任何命令(尽管@Antirez暗示将来会进行订户安全的ping操作)。如果您尝试做其他事情,redis会告诉您:

-ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context
(note that you can't test this with redis-cli, since that understands the protocol well enough to prevent you from issuing commands once you have subscribed - but any other basic socket tool should work fine)

这是因为订户连接的工作方式非常不同-而不是根据请求/响应进行工作,现在传入的消息可以随时主动发出。

publish是常规的请求/响应命令,因此必须在常规连接而不是订户连接上发送。
有关更多信息,请单击此处