What Is Workerman and How to Build a Simple PHP Server with It

Workerman is an open‑source, pure‑PHP high‑performance application container that acts like a PHP‑based Nginx, enabling developers to create TCP/UDP, WebSocket, HTTP services and even custom servers such as Redis or FTP, with multi‑process, epoll and non‑blocking I/O support.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
What Is Workerman and How to Build a Simple PHP Server with It

What Is Workerman?

Workerman is an open‑source, pure‑PHP high‑performance application container. It is not an MVC framework but a low‑level, generic service framework that lets developers build TCP proxies, ladder proxies, game servers, mail servers, FTP servers, and even PHP implementations of Redis, databases, Nginx, or php‑fpm.

Internally it works like a PHP version of Nginx: each worker process runs multiple processes using epoll and non‑blocking I/O, allowing each process to maintain tens of thousands of concurrent connections. Because it runs as a resident process, it does not depend on Apache, Nginx, or php‑fpm, delivering very high performance. It supports TCP, UDP, UNIX sockets, long‑living connections, WebSocket, HTTP, WSS, HTTPS and custom protocols, and provides timers, asynchronous socket clients, async Redis, async HTTP, and async message queues.

What Is the Worker Class?

Workerman defines two key classes: Worker and Connection. A Worker object acts as a container that listens on a specific protocol and port. When a client connects, a Connection object is created inside the worker, and developers manipulate this object to send and receive data.

Simple Example

<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;

require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('http://0.0.0.0:8686');

$worker->onWorkerStart = function ($worker) {
    // initialization code
};

$worker->onConnect = function ($connection) {
    echo "connection success" . PHP_EOL;
};

$worker->onMessage = function (TcpConnection $connection, Request $request) {
    $connection->send("Hi 开源技术小栈");
};

$worker->onClose = function ($connection) {
    echo "connection close" . PHP_EOL;
};

$worker->onWorkerStop = function ($worker) {
    // cleanup code
};

// Run all workers
Worker::runAll();
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentPHPNetworkinghigh performanceWorkerman
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.