WsServer (WebSocket server)

Purpose of this Component

This component (when used with IoServer) allows your server to communicate with web browsers that use the W3C WebSocket API. It is compatible with each of the different WebSocket protocol versions browsers use: RFC6455, HyBi, and Hixie76. Ratchet passes all (non-binary) WebSocket protocol tests from the Autobahn Testsuite.

Note: This component created for you when you add a route to an App instance.

Events triggered by this Component

As found in the API Docs: Triggered events are propagated through a MessageComponentInterface object passed to the __construct.

  • onOpen (ConnectionInterface $conn) - A new client connection has been opened
  • onClose (ConnectionInterface $conn) - A client connection is about to, or has closed
  • onMessage (ConnectionInterface $from, string $message) - A data message has been received
  • onError (ConnectionInterface $from, Exception $error) - An error has occurred with a Connection

Configuration methods

  • void disableVersion (string $versionId) - Disable a specific version of the WebSocket protocol. Sometimes you'll want to disable Hixie76, which you can do by passing 0 to this method.
  • void setEncodingChecks (boolean $opt) - Turn UTF-8 checks on or off. The WebSocket spec says to check incoming messages for UTF-8 encoding, but all web browsers do this. Turning off this check will boost performance.

Functions callable on Connections

  • send (string $message) - Send a message (string) to the client
  • close - Gracefully close the connection to the client

Parameters added to each Connection

(Guzzle\Http\Message\RequestInterface $request) - A Guzzle Request object containing all the information from the initial HTTP connection

Wraps other components nicely

Wrapped by other components nicely


// Your shell script
use Ratchet\WebSocket\WsServer;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;

    $ws = new WsServer(new MyChat);
    $ws->disableVersion(0); // old, bad, protocol version

    // Make sure you're running this as root
    $server = IoServer::factory(new HttpServer($ws));