Php uses swoole to build a websocket server. After the front-end page builds a persistent connection, how does the back-end actively send data to the front-end (how to trigger)

I built a socket server to receive data from the hardware. Process and pass in the database.
I want to display data on the front end in real time through websocket. How to do it.

how to send a command to websocket in socket, telling websocket to send data.
or how to let websocket know that socket received the data and processed it.

originally wanted to open an extra thread through websocket. Without affecting the sending and receiving data of websocket, lpop waits for the queue, but does not know how to open this thread. Ask for advice!

Jan.19,2022

now one of my solutions is to use redis's subscription


1, where is the thread of PHP? Even if you use swoole, where is there any thread
2, followed by this problem suggests you to cooperate with a structure diagram, and scene users

I unilaterally think that your front-end page is for internal use, so I don't think you need to use any websocket,AJAX polling to check the database, that is to say, the process of the front-end is set back, which seems to solve your problem, and insiders can bear to use AJAX to poll the general database.

if you have to do what you say, just introduce a layer of message queues. After receiving the request from the hardware, socket "double writes", one to the database and the other to the message queue, acting as a producer. At the other end of the message queue, websocket can act as a consumer, constantly reading messages from the message queue and then send it to the front end. Message queue can be used as redis, no problem.

[Editor's Note: in order to prevent being entangled by the rod essence, add: PHP has multithreading and pthread extension; there are also multithreads in swoole, that is, reactor threads in the master process]


when I write a cookie, front end to establish a link, I use cookie as the key to write down the link. When the front end is disconnected, clear this link, or clear it after a certain period of time. When I need to send a message, use cookie to find the link to send a message, or directly cycle through it all


swoole to listen for an extra udp port

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37bdc-2c0af.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37bdc-2c0af.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?