Swoole and Workerman's message broadcast and subscription real-time notification function in PHP and MySQL
Abstract:
With the rapid development of the Internet, real-time notification function It has also become an integral part of modern applications. In PHP and MySQL, we can use Swoole and Workerman, two excellent extension libraries, to implement message broadcasting and subscription to achieve real-time notification functions. This article will introduce the application scenarios of Swoole and Workerman in PHP and MySQL in detail, and give specific code examples to help readers understand and practice the real-time notification function.
<?php $server = new SwooleWebSocketServer("0.0.0.0", 9501); $server->on('open', function (SwooleWebSocketServer $server, $request) { echo "new connection open: {$request->fd} "; }); $server->on('message', function (SwooleWebSocketServer $server, $frame) { $message = $frame->data; // 实现消息广播 foreach($server->connections as $fd) { $server->push($fd, $message); } }); $server->on('close', function ($ser, $fd) { echo "connection close: {$fd} "; }); $server->start();
In the above example, we created a Swoole WebSocket server and listened to events via on('message')
, implements the message broadcast function. When a new connection is established, the connection ID will be printed; when a message is received, all connections will be traversed and a message will be sent to each connection.
<?php require_once './Workerman/Autoloader.php'; use WorkermanLibTimer; use WorkermanWorker; $worker = new Worker("websocket://0.0.0.0:2345"); $worker->onWorkerStart = function () { Timer::add(1, function () { // 实现消息广播 foreach (Worker::$worker[0]->connections as $connection) { $connection->send('Hello'); } }); }; $worker->onConnect = function ($connection) { echo "New connection "; }; $worker->onMessage = function ($connection, $data) { echo "Receiving message: {$data} "; }; $worker->onClose = function ($connection) { echo "Connection closed "; }; Worker::runAll();
In the above example, we created a Workerman WebSocket server and implemented it through the Timer::add()
method The function of sending messages regularly to realize message broadcast. When a new connection is established, relevant information will be printed; when a message is received, the message content will be printed; when the connection is closed, the corresponding information will be printed.
CREATE TABLE `messages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `content` varchar(255) NOT NULL, PRIMARY KEY (`id`) ); CREATE TRIGGER `new_message` AFTER INSERT ON `messages` FOR EACH ROW BEGIN DECLARE message VARCHAR(255); SET message = CONCAT('New message: ', NEW.content); -- 发送实时通知 INSERT INTO `notifications` (`message`) VALUES (message); END;
Through the above trigger definition, when a new message is inserted into the messages
table, the code in the trigger will be automatically triggered and the message information will be inserted into notifications
table. Then in the Swoole or Workerman server, implement the function of regularly querying the notifications
table, and when there is a new notification, send it to the corresponding client.
The above is the detailed content of Swoole and Workerman's message broadcast and subscription real-time notification functions in PHP and MySQL. For more information, please follow other related articles on the PHP Chinese website!