Search code examples
phplaravelazurewebsocketratchet

How can I set Realtime WebSocket buffer?


I am working on a realtime multiplayer game which was built using Ratchet 0.3.3, Laravel 5 and PHP 5.5.9, The server OS is Ubuntu. The server sends approximately 500 bytes of data in each cycle to every user via WebSocket (hundreds of users). It looks like WebSocket is buffering the requests and sends 5 to 6 requests at once (500 bytes each). We have 30 millisecond cycles. Is there a way to manually set the WebSocket buffer settings , so my requests can be sent with no delays ?


Solution

  • I found a solution for that problem. First, IoServer class created and extend IoServer from Ratchet.

    class IoServer extends \Ratchet\Server\IoServer {
    
        public static function factory(MessageComponentInterface $component, $port = 80, $address = '0.0.0.0') {
            $loop   = LoopFactory::create();
            $socket = new Reactor($loop);
            $socket->listen($port, $address);
            $sock = socket_import_stream($socket->master);
            socket_set_option($sock, SOL_TCP, TCP_NODELAY, true);
    
            return new static($component, $socket, $loop);
        }
    }