Current Location: Home> Latest Articles> Swoole and Workerman: A Practical Guide to Asynchronous Message Handling in PHP and MySQL

Swoole and Workerman: A Practical Guide to Asynchronous Message Handling in PHP and MySQL

M66 2025-10-30

Asynchronous Message Handling in PHP with Swoole and Workerman

Swoole and Workerman are two widely used asynchronous network frameworks in PHP that can significantly improve performance when interacting with MySQL databases. This article focuses on their asynchronous message handling capabilities and provides practical code examples for developers.

Swoole Asynchronous Message Handling

Swoole is a production-ready asynchronous network communication engine for PHP that leverages coroutine technology to achieve high-performance asynchronous I/O operations. It natively supports the MySQL protocol, allowing developers to perform database operations asynchronously and improve query efficiency.

Example: Asynchronous MySQL Query with Swoole

<?php
$server = new SwooleCoroutineMySQL();
$server->connect([
    'host' => '127.0.0.1',
    'port' => 3306,
    'user' => 'username',
    'password' => 'password',
    'database' => 'dbname',
]);

SwooleRuntime::enableCoroutine();

go(function() use ($server) {
    $result = $server->query('SELECT * FROM table1');
    var_dump($result);
});

go(function() use ($server) {
    $result = $server->query('SELECT * FROM table2');
    var_dump($result);
});

SwooleEvent::wait();
?>

In this code, a Swoole MySQL object is created and connected to the database. Using go() functions, two asynchronous queries are executed in coroutines, and SwooleEvent::wait() ensures that all coroutines complete.

Workerman Asynchronous Message Handling

Workerman is another high-performance asynchronous PHP communication framework that uses an event-driven model, making network programming flexible. It also supports asynchronous I/O operations, suitable for high-concurrency scenarios.

Example: Asynchronous MySQL Query with Workerman

<?php
require_once __DIR__ . '/vendor/autoload.php';
use WorkermanMySQLConnection;

$mysql = new Connection('127.0.0.1', '3306', 'username', 'password', 'dbname');
$worker = new Worker();

$worker->onWorkerStart = function() use ($mysql) {
    $result = $mysql->query('SELECT * FROM table1');
    var_dump($result);

    $result = $mysql->query('SELECT * FROM table2');
    var_dump($result);
};

Worker::runAll();
?>

In this example, the Workerman framework is loaded via autoload, a MySQL connection object is created, and asynchronous queries are executed in the Worker object's onWorkerStart callback. Worker::runAll() starts the event loop.

Conclusion

Both Swoole and Workerman efficiently support asynchronous message handling between PHP and MySQL. Developers can choose the appropriate framework based on project requirements to achieve fast and efficient asynchronous database interactions. The code examples provided here serve as references for beginners to better understand the implementation of asynchronous message handling in practical applications.