Skip to content

Core\Http\Socket::protocol

$socket->protocol(): ?string

Returns the subprotocol the server chose when the WebSocket connection was opened, or null.

A subprotocol is a name for the message format both sides use, for example chat.v2. Your program offers one or more names with the protocols option of Core\Http\Client::openSocket(). The server chooses one of them, or none. protocol() returns the name it chose. It returns null when your program offered no names or the server chose none.

If the server chooses a name your program did not offer, openSocket() throws an error. So a name that protocol() returns is always one of the names you offered. The name stays readable after close().

The examples below read the chosen name, handle a server that chose none, and pick a message format from the name.

Return value

Returns ?string. The name the peer chose. It is null if the call offered no protocol or the peer chose none. If the peer chooses a name that was not offered, the handshake fails and no socket is returned.

Examples

Reading the chosen name

<?nvs
// Shows how to read the subprotocol the server chose from the names your program offered.
// A fixed server, so the example runs without a network. It chooses `chat.v2`.
Core\Test::answerSocket('wss://chat.example.com/room', ['welcome'], { protocol: 'chat.v2' });
// The program offers two names. The server chooses one of them.
Core\Http\Socket $socket = Core\Http\Client::openSocket('wss://chat.example.com/room', {
protocols: ['chat.v1', 'chat.v2'],
});
// This prints `chat.v2`.
echo $socket->protocol() ?? 'none', "\n";
Output
chat.v2

When no name was chosen

<?nvs
// Shows that `protocol()` returns `null` when no subprotocol was chosen.
// Two fixed servers, so the example runs without a network. Neither chooses a name.
Core\Test::answerSocket('wss://news.example.com/live', ['headline']);
Core\Test::answerSocket('wss://chat.example.com/room', ['welcome']);
// This program offers no names, so the result is `null`. This prints `none`.
Core\Http\Socket $plain = Core\Http\Client::openSocket('wss://news.example.com/live');
echo $plain->protocol() ?? 'none', "\n";
// This program offers a name, but the server does not choose it.
// The result is also `null`. This prints `none`.
Core\Http\Socket $offered = Core\Http\Client::openSocket('wss://chat.example.com/room', {
protocols: ['chat.v2'],
});
echo $offered->protocol() ?? 'none', "\n";
Output
none
none

Picking a message format

<?nvs
// Shows a client that supports two versions of a message format
// and writes each message in the version the server chose.
class Chat
{
// Returns one chat message in the format that `$protocol` names.
public static function message(?string $protocol, string $text): string
{
if ($protocol == 'chat.v2') {
return Core\Json::encode({ type: 'say', text: $text });
}
// Version 1, and servers that chose no name, use plain text.
return 'SAY ' . $text;
}
}
// Two fixed servers, so the example runs without a network.
Core\Test::answerSocket('wss://new.example.com/chat', [], { protocol: 'chat.v2' });
Core\Test::answerSocket('wss://old.example.com/chat', [], { protocol: 'chat.v1' });
// This prints one line per server: the chosen name, then the message in that format.
array<string> $servers = ['wss://new.example.com/chat', 'wss://old.example.com/chat'];
foreach ($servers as string $url) {
Core\Http\Socket $socket = Core\Http\Client::openSocket($url, { protocols: ['chat.v1', 'chat.v2'] });
string $line = Chat::message($socket->protocol(), 'hello');
$socket->send($line);
echo $socket->protocol() ?? 'none', ': ', $line, "\n";
$socket->close();
}
Output
chat.v2: {"text":"hello","type":"say"}
chat.v1: SAY hello