Skip to main content

nexus-http-server-swoole-threads

Thread-mode HTTP and WebSocket server built on Swoole 6's SWOOLE_THREAD runtime, enabling cross-thread actor pools, channel-backed WebSocket broadcasts, and lock-free async logging.

What's in this package

Requirements

  • PHP 8.5+ compiled with ZTS (Zend Thread Safety)
  • Swoole 6.0+ compiled with --enable-swoole-thread

Install

composer require nexus-actors/http-server-swoole-threads

Quick example

src/Server/boot.php
use Monadial\Nexus\Core\Actor\ActorSystem;
use Monadial\Nexus\Http\Response\JsonResponse;
use Monadial\Nexus\Http\Server\Swoole\Threads\Server\SwooleThreadConfig;
use Monadial\Nexus\Http\Server\Swoole\Threads\Server\SwooleThreadServer;
use Monadial\Nexus\Http\Ws\CompiledApplication;
use Monadial\Nexus\Http\Ws\WsApplication;
use Monadial\Nexus\Runtime\Duration;
use Monadial\Nexus\WorkerPool\WorkerNode;

SwooleThreadServer::run(
SwooleThreadConfig::bind('0.0.0.0', 8080)
->threads(8)
->enableWebSocket(true)
->shutdownTimeout(Duration::seconds(5)),
static function (ActorSystem $system, WorkerNode $node): CompiledApplication {
return WsApplication::create($system)
->get('/', static fn() => JsonResponse::ok(['tid' => $node->workerId()]))
->compile();
},
);

The factory closure runs once per thread and receives a fresh ActorSystem and a WorkerNode identifying the thread within the pool.

Thread mode is the only Swoole adapter that supports $app->channel(...) routes, because channel actor state must outlive a single Swoole worker process. Use nexus-http-server-swoole (worker mode) for simpler deployments that do not need cross-thread shared state.

See also