Getting Started with Swoole for IoT Development: Installation, TCP & WebSocket Servers, Async I/O, and Coroutines
This guide introduces Swoole, a high‑performance asynchronous PHP framework for IoT, and walks through installing the extension, building TCP and WebSocket servers, performing asynchronous file I/O, and leveraging coroutines with complete code examples.
Swoole is an asynchronous, high‑performance network communication framework for PHP that supports TCP, UDP, WebSocket, coroutine, asynchronous file I/O, timers, and is well‑suited for IoT development.
1. Install the Swoole extension
Swoole requires PHP 7 or higher. You can install it via PECL: pecl install swoole Or compile from source:
git clone https://github.com/swoole/swoole-src.git
cd swoole-src
phpize
./configure
make
sudo make installAfter installation, add the extension to your php.ini: extension=swoole.so 2. Create a TCP Server
The following code creates a simple TCP server that listens on 127.0.0.1:9501 and handles connect, receive, and close events:
$server = new \Swoole\Server('127.0.0.1', 9501);
$server->on('connect', function ($server, $fd) {
echo "Client: Connect.
";
});
$server->on('receive', function ($server, $fd, $from_id, $data) {
$server->send($fd, 'Swoole: ' . $data);
});
$server->on('close', function ($server, $fd) {
echo "Client: Close.
";
});
$server->start();3. Create a WebSocket Server
Swoole also supports the WebSocket protocol. The code below creates a WebSocket server on the same address and port, handling open, message, and close events:
$server = new \Swoole\Websocket\Server('127.0.0.1', 9501);
$server->on('open', function ($server, $request) {
echo "Client: Connect.
";
});
$server->on('message', function ($server, $frame) {
$server->push($frame->fd, 'Swoole: ' . $frame->data);
});
$server->on('close', function ($server, $fd) {
echo "Client: Close.
";
});
$server->start();4. Use Asynchronous I/O
Swoole provides asynchronous file operations. The example reads a file without blocking the event loop:
$file = '/path/to/file';
\Swoole\Async::readFile($file, function ($filename, $content) {
echo $content;
});5. Use Coroutines
Coroutines allow writing asynchronous code in a synchronous style. The following snippet creates a coroutine that opens a TCP client, sends a message, and receives a response:
go(function () {
$client = new \Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
$client->connect('127.0.0.1', 9501);
$client->send('Hello, Swoole!');
$response = $client->recv();
echo $response;
});These steps demonstrate how to set up Swoole for IoT projects, covering installation, server creation, asynchronous I/O, and coroutine usage.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
