Notice: ext-swoole is supported until v4.7.1, use ext-openswoole >= v4.7.1. Latest version: pecl install openswoole-4.9.1


WebSocket data frame object. A Swoole WebSocket server since v4.2.0 will use the Swoole\WebSocket\Frame object to represent WebSocket data and other attributes about the request and its data. This object is used so that it is easy to access WebSocket data but also an easy way to know where the data came from, using the provided $fd property and other information about the frame. Both the server and the client Swoole services use this object.

Note: Since v4.4.12 the WebSocket service no longer uses the bool $finish parameter, instead it has been converted to int $flags so that multiple states can be represented as integers and constants. See the WebSocket pack() method for more information on the finish and compression flags.

You can enable data frame compression, see the documentation for it here.

Object Structure

Common structure for the Swoole Frame object:

object(Swoole\WebSocket\Frame) (4) {
  ["fd"]      =>  int(0)
  ["data"]    =>  NULL
  ["opcode"]  =>  int(1)
  ["finish"]  =>  bool(true)
  • Swoole\WebSocket\Frame->fd: The ID of the WebSocket client connection, this can be used to push data to the client
  • Swoole\WebSocket\Frame->data: The data received from the client, check the OpCodes for the different data types that can be expected
  • Swoole\WebSocket\Frame->opcode: The opcode type of WebSocket data frame, type of data received
  • Swoole\WebSocket\Frame->finish: If the data frame is complete or not, this can be used to indicate if data is being split up into frames or if the server has a complete frame, sometimes if the server is receiving a lot of frames, it will report the data as null if finish is false, this is because the server is still merging incoming data into one frame

WebSocket Frame OpCodes

When sending data over WebSockets, you can set the format that the data is in, Swoole has predefined constants which represent each type of data which can be sent/received.

  • WEBSOCKET_OPCODE_TEXT = 1: UTF-8 encoded text data
  • WEBSOCKET_OPCODE_BINARY = 2: Binary data
  • WEBSOCKET_OPCODE_CLOSE = 8: Closed frame data type and object
  • WEBSOCKET_OPCODE_PING = 9: Ping data
  • WEBSOCKET_OPCODE_PONG = 10: Pong data



use Swoole\WebSocket\Server;
use Swoole\Http\Request;
use Swoole\WebSocket\Frame;

$server = new Server("", 9501);

$server->on("Start", function(Server $server)
    echo "Swoole WebSocket Server started at\n";

$server->on('Open', function(Server $server, Swoole\Http\Request $request)
    echo "connection open: {$request->fd}\n";

    $server->tick(1000, function() use ($server, $request)
        $server->push($request->fd, json_encode(["hello", time()]));

$server->on('Message', function(Server $server, Frame $frame)
    echo "received message: {$frame->data}\n";
    $server->push($frame->fd, json_encode(["hello", time()]));

$server->on('Close', function(Server $server, int $fd)
    echo "connection close: {$fd}\n";