Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5


WebSocket data frame object. A OpenSwoole WebSocket server since v4.2.0 will use the OpenSwoole\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 OpenSwoole 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 OpenSwoole Frame object:

object(OpenSwoole\WebSocket\Frame) (4) {
  ["fd"]      =>  int(0)
  ["data"]    =>  NULL
  ["opcode"]  =>  int(1)
  ["finish"]  =>  bool(true)
  • OpenSwoole\WebSocket\Frame->fd: The ID of the WebSocket client connection, this can be used to push data to the client
  • OpenSwoole\WebSocket\Frame->data: The data received from the client, check the OpCodes for the different data types that can be expected
  • OpenSwoole\WebSocket\Frame->opcode: The opcode type of WebSocket data frame, type of data received
  • OpenSwoole\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, OpenSwoole 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 OpenSwoole\WebSocket\Server;
use OpenSwoole\Http\Request;
use OpenSwoole\WebSocket\Frame;

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

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

$server->on('Open', function(Server $server, OpenSwoole\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";

Last updated on September 1, 2022