Alternative Way To Resolve Stream_select You Must Recompile Php With A Larger Value Of Fd_setsize Issue Without Recompiling Php
I met a boring issue when adding websocket to a PHP site by using Ratchet (A PHP websocket framework):
“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”
I struggled and googled it, finally found a bad news: FD_SIZE is set to 1024 in the PHP source code. The error occurs if the web socket connections are over 1024. I don’t want to modify the PHP source code and recompile it, which may cause unexpected issue. So I try to find another way to solve this issue. Fortunately, I found it.
Basically, I’m opening multiple websocket service processes in the server, and each process has different port. In the front end, I wrote a js function to choose different port to connect. That means we have 1024 * n availiable connections now. Let’s go to the details.
Back-end implementation
Write a websocket service called push-server.php. Please refer to Ratchet’s example. However, we need make the php file to accept a port parameter:
$port
=
$argv
[1];
if
(
$port
==
""
){
$port
= 40003;
// Default port
}
// .....Ignoring other code .....
// Passing port paramter.
$webSock
->listen(
$port
,
'0.0.0.0'
);
function getWSServer() { var serverPorts = ['40003', '40004', '40005']; var server = 'ws://youhost'; var randomPortIndex = Math.floor(Math.random() * serverPorts.length); server += ':' + serverPorts[randomPortIndex]; return server; };