Swoole Server sendMessage

bool Swoole\Server::sendMessage ( int $worker_id , string $data )

Send message to worker processes by ID. This message will trigger the event of pipe message and the process which has received this message would call the callback function registered for pipe message.

There is no length limit for the message, but the server will use the temporary file in the memory if the length of file is bigger than 8K In the task worker process, sendMessage is blocking In the worker process, sendMessage is async.

Parameter

  • $message the string of message to send
  • $dst_worker_id the integer of worker process which is between 0 and (worker_num + task_worker_num - 1)

Return

The result of send message to worker

Example

<?php
$server = new Swoole\Server("0.0.0.0", 9501);
$server->set(array(
    'worker_num' => 2,
    'task_worker_num' => 2,
));
$server->on('pipeMessage', function($server, $src_worker_id, $data) {
    echo "#{$server->worker_id} message from #$src_worker_id: $data\n";
});
$server->on('task', function ($server, $task_id, $from_id, $data){
    var_dump($task_id, $from_id, $data);
});
$server->on('finish', function ($server, $fd, $from_id){

});
$server->on('receive', function (Swoole\Server $server, $fd, $from_id, $data) {
    if (trim($data) == 'task')
    {
        $server->task("async task coming");
    }
    else
    {
        $worker_id = 1 - $server->worker_id;
        $server->sendMessage("hello task process", $worker_id);
    }
});

$server->start();