About PHP Websocket error: " stream_select (You): MUST recompile PHP with a

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Recently on the use of Ratchet (a PHP websocket) when the transformation of a PHP site, the wrong:

"It is set to 1024, but you have descriptors numbered at least as high as 1266.

--enable-fd-setsize=2048 is recommended, but you may want to set it
to equal the maximum number of open files supported by your system"

After several setbacks, found the reason is PHP source code set FD_SIZE to 1024, once more than 1024 websocket connection, will report this error. Solution of on-line, basically is to modify the PHP source code, but personally think that modify the PHP source code is recompiled PHP. risk, we put forward new ideas, and successfully used in the project (applause).

The basic principle is in the background of several websocket service processes, each process using a different port number. And the JS random access backstage open websocket service port number. So that each websocket service process with 1024 of the available number of connections, according to the website of the peak number of connections can be calculated to the required number of open websocket service process, of course, need to keep some allowance. Do not need to re compile the PHP source code, can easily break through PHP websocket 1024 connection limit.

The background of the implementation of

Write a push-server.php as a websocket service, the implementation of push-server can refer to the example of Ratchet, but needs a little modification, which can enter port as the command line parameters.

$port = $argv[1]; if ($port == ""){ $port = 40003; // The default port, if you start push-server.php does not write the parameters, then use port 40003} //..... Omit other code.... // $port as the port monitor incoming $webSock->listen($port, '');


According to the need, you can start multiple websocket processes, such as:

php push-server 40003

php push-server 40004

php push-server 40005

This background can allow 1024 * 3 = 3072 websocket connections.

The implementation of

Random access connection service code:

function getWSServer() {
    var serverPorts = ['40003', '40004', '40005'];
    var server = 'ws://youhost';
    var randomPortIndex = Math.floor(Math.random() * serverPorts.length);
    server += ':' + serverPorts[randomPortIndex];
    return server;

Of course, you can also according to their own needs, specify what time to connect to a websocket service, no longer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Rick at March 28, 2014 - 8:05 AM