How does swoole proactively push messages to clients?

there is $serv in the callback function of swoole, such as onOpen onMessage, but what if you want to push a message to the client somewhere else?

used to use $serv- > push ($fd,$data);

but how to solve this if serv doesn"t exist anywhere else?

May.31,2021

put it simply: "somewhere else"-"connect to server -" $serv- > push ($fd,$data);
details: in other php-fpm programs, you need tcp to connect to the service started through swoole, and then find the user you want to send ($fd), and then send


actually there is an onRequest callback in swoole, just listen in this callback.
complete the logic with parameters through the crul request. For example: http://www.demo.com:9501/?param=123456


first of all, I don't know where else you're talking about, specifically how the code is organized. Well, without the code, I can only guess.

you may want to use this server, in controller, but controller may be called by the swoole file through call_user_func.

in this case, you can pass serv directly as a parameter to the business code by passing parameters;
then, in addition, you can also try global;


thanks for the invitation. You can try listening to an event on a service with $serv, and then calling it somewhere else. $serv receives the listener and starts processing logic


I just had a problem with this scenario.

my assumption is that by building a websocket client connection on the server side to request, the business can still be written in onMessage.

for example, if I make a controller, of a scheduled task to trigger an active push to someone in a scheduled task, this controller must have no websocket server method, but can only build a websocket client in controller.

Menu