Pipe Messages
When Swoole Server starts, it spawns multiple workers to handle incoming HTTP requests. If you want to communicate with those workers, you have to use some kind of IPC mechanism (unix sockets, dgram, etc).
Pipe messages feature provides a way to register both messages handlers and issuers.
Usage
Registering Message Handlers
You need to add HandlesServerPipeMessage
attribute and to implement
ServerPipeMessageHandlerInterface
:
phpuse Distantmagic\Resonance\Attribute\GrantsFeature; use Distantmagic\Resonance\Attribute\HandlesServerPipeMessage; use Distantmagic\Resonance\Attribute\Singleton; use Distantmagic\Resonance\ServerPipeMessageHandlerInterface; #[GrantsFeature(Feature::WebSocket)] #[HandlesServerPipeMessage(MyPipeMessage::class)] #[Singleton(collection: SingletonCollection::ServerPipeMessageHandler)] class MyServerPipeMessageHandler implements ServerPipeMessageHandlerInterface { /** * @param CloseWebSocketConnection $serverPipeMessage */ public function handleServerPipeMessage(ServerPipeMessageInterface $serverPipeMessage): void { // ... } }
Dispatching Pipe Messages
You need to have direct access to the Server
object:
phpuse Distantmagic\Resonance\ServerPipeMessageInterface; use Swoole\Http\Server; /** * @var Server $server * @var ServerPipeMessageInterface $message * @var int $workerId */ $server->sendMessage($message, $workerId);