9951 explained code solutions for 126 technologies

php-swooleWebsocket server example

$ws = new Swoole\WebSocket\Server('', 9504);
  function (Swoole\WebSocket\Server $ws, Swoole\WebSocket\Frame $frame) {
    $ws->push($frame->fd, "Hello, {$frame->data}");
$ws->start();ctrl + c

Swoole class to launch Websocket server


listen on all interfaces


listen on this port


listen for specific event on server


do something when we receive message from client


send response to client

"Hello, {$frame->data}"

response message we want to send ($frame->data will contain message received from client)