An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.
An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.
Workerman is an asynchronous event-driven PHP framework with high performance to build fast and scalable network applications. It supports HTTP, WebSocket, custom protocols, coroutines, and connection pools, making it ideal for handling high-concurrency scenarios efficiently.
A POSIX compatible operating system (Linux, OSX, BSD)
POSIX and PCNTL extensions required
Event/Swoole/Swow extension recommended for better performance
composer require workerman/workerman<?php
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
// Create a Websocket server
$ws_worker = new Worker('websocket://0.0.0.0:2346');
// Emitted when new connection come
$ws_worker->onConnect = function ($connection) {
echo "New connection\n";
};
// Emitted when data received
$ws_worker->onMessage = function ($connection, $data) {
// Send hello $data
$connection->send('Hello ' . $data);
};
// Emitted when connection closed
$ws_worker->onClose = function ($connection) {
echo "Connection closed\n";
};
// Run worker
Worker::runAll();use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
// #### http worker ####
$http_worker = new Worker('http://0.0.0.0:2345');
// 4 processes
$http_worker->count = 4;
// Emitted when data received
$http_worker->onMessage = function ($connection, $request) {
//$request->get();
//$request->post();
//$request->header();
//$request->cookie();
//$request->session();
//$request->uri();
//$request->path();
//$request->method();
// Send data to client
$connection->send("Hello World");
};
// Run all workers
Worker::runAll();………Coroutine is used to create coroutines, enabling the execution of asynchronous tasks to improve concurrency performance.
<?php
use Workerman\Connection\TcpConnection;
use Workerman\Coroutine;
use Workerman\Events\Swoole;
use Workerman\Protocols\Http\Request;
use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8001');
$worker->eventLoop = Swoole::class; // Or Swow::class or Fiber::class
$worker->onMessage = function (TcpConnection $connection, Request $request) {
Coroutine::create(function () {
echo file_get_contents("http://www.example.com/event/notify");
});
$connection->send('ok');
};
Worker::runAll();Note: Coroutine require Swoole extension or Swow extension or Fiber revolt/event-loop, and the same applies below
Barrier is used to manage concurrency and synchronization in coroutines. It allows tasks to run concurrently and waits until all tasks are completed, ensuring process synchronization.
…Parallel executes multiple tasks concurrently and collects results. Use add to add tasks and wait to wait for completion and get results. Unlike Barrier, Parallel directly returns the results of each task.
…Channel is a mechanism for communication between coroutines. One coroutine can push data into the channel, while another can pop data from it, enabling synchronization and data sharing between coroutines.
…Pool is used to manage connection or resource pools, improving performance by reusing resources (e.g., database connections). It supports acquiring, returning, creating, and destroying resources.
……php start.php start php start.php start -d php start.php status php start.php status -d php start.php connectionsphp start.php stop php start.php stop -g php start.php restart php start.php reload php start.php reload -g
https://www.techempower.com/benchmarks/#section=data-r19&hw=ph&test=plaintext&l=zik073-1r
Workerman is released under the MIT license.
We need to investigate the new poll_api from PHP/8.6
Swoole6.2.1 事件循环下 HTTPS 请求超时:stream_socket_enable_crypto() 返回 false
[BUG] eventLoop设为Swoole,当Swoole版本为6.x在非iouring下响应大文件异常的BUG
Add new event engine io_uring [sugestion]
[Feature request?] Add Support Transfer-Encoding chunked in Request
HTTP 1.1 use by default persistent connections
Low performance if using Nginx as a reverse proxy
websocket的onMessage方法中是否有办法知道客户端发送的消息是text类型还是binary类型?
[Feature request?] Support Transfer-Encoding chunked with no Content-Length
[feature request?] Worker count scale