Client • Server • MessageClassesExamplesChangelogContributing

The library contains a rudimentary single stream/single thread server. It internally supports Upgrade handshake and implicit close and ping/pong operations.

Note that it does not support threading or automatic association ot continuous client requests. If you require this kind of server behavior, you need to build it on top of provided server implementation.

Examples

Simple receive-send operation

This example reads a single message from a client, and respond with the same message.

$server = new WebSocket\Server();
$server->accept();
$message = $server->receive();
$server->text($message);
$server->close();

Listening to clients

To continuously listen to incoming messages, you need to put the receive operation within a loop. Note that these functions always throw exception on any failure, including recoverable failures such as connection time out. By consuming exceptions, the code will re-connect the socket in next loop iteration.

$server = new WebSocket\Server();
while ($server->accept()) {
    try {
        $message = $server->receive();
        // Act on received message
        // Break while loop to stop listening
    } catch (\WebSocket\ConnectionException $e) {
        // Possibly log errors
    }
}
$server->close();

Receiving messages

By default the receive() method return messages of 'text' and 'binary' opcode. The filter option allows you to specify which message types to return.

$server = new WebSocket\Server(['filter' => ['text']]);
$server->receive(); // only return 'text' messages

$server = new WebSocket\Server(['filter' => ['text', 'binary', 'ping', 'pong', 'close']]);
$server->receive(); // return all messages

By setting an option, receive() will instead return a Message instance.

$server = new WebSocket\Server(['return_obj' => true]);
$message = $server->receive();
$message->getOpcode();
$message->getContent();

Sending messages

There are convenience methods to send messages with different opcodes.

$server = new WebSocket\Server();

// Convenience methods
$server->text('A plain text message'); // Send an opcode=text message
$server->binary($binary_string); // Send an opcode=binary message
$server->ping(); // Send an opcode=ping frame
$server->pong(); // Send an unsolicited opcode=pong frame

// Generic send method
$server->send($payload); // Sent as opcode=text
$server->send($payload, 'binary'); // Sent as opcode=binary

// Message send method
$server->send(new WebSocket\Message\Text($payload)); // Sent as opcode=text
$server->send(new WebSocket\Message\Binary($payload)); // Sent as opcode=text

Constructor options

The $options parameter in constructor accepts an associative array of options.

  • filter - Array of opcodes to return on receive, default ['text', 'binary']
  • fragment_size - Maximum payload size. Default 4096 chars.
  • logger - A PSR-3 compatible logger.
  • port - The server port to listen to. Default 8000.
  • return_obj - Return a Message instance on receive, default false
  • timeout - Time out in seconds. Default 5 seconds.
$server = new WebSocket\Server([
    'filter' => ['text', 'binary', 'ping'], // Specify message types for receive() to return
    'logger' => $my_psr3_logger, // Attach a PSR3 compatible logger
    'port' => 9000, // Listening port
    'return_obj' => true, // Return Message instance rather than just text
    'timeout' => 60, // 1 minute time out
]);

Exceptions

  • WebSocket\BadOpcodeException - Thrown if provided opcode is invalid.
  • WebSocket\ConnectionException - Thrown on any socket I/O failure.
  • WebSocket\TimeoutException - Thrown when the socket experiences a time out.