10

I currently am running a Debian 8 machine with PHP, Apache on it.

On this machine I have installed a ratchet websocket server although I recently went over 1000 concurrent users on the website I am running. This caused problems with the Ratchet socket since at 1020 connections it seems to automatically refuse all new connections.

I already increased the ulimit -n and sortlike for debian itself, but I started to wonder if the problem might be in my php default configuration or the ratchet websocket I am using.

Any ideas on what configuration I should change are highle appreciated.

mitchken
  • 790
  • 9
  • 26
  • Can anyone confirm whether is it a good idea to implement WebSockets via PHP & Ratchet? Is there any downfall to this approach? – Airy Nov 20 '21 at 09:55
  • The honest answer here is that PHP was a wrong choice for me, check out (socket.io or sockjs) in NodeJS, python and go also have some great frameworker/libs for this. Just my thought after that project. – mitchken Nov 21 '21 at 12:48
  • thank you for your kind suggestion. Can you tell my exactly why it was a wrong choice as php for websockets? It would be really helpful to learn from your experience. – Airy Nov 22 '21 at 05:05
  • The lack of documentation, missing event handling options and larger hurdles by using PHP forced me to move over to NodeJS which has well documented broadly used websocket implementations. – mitchken Nov 22 '21 at 09:38
  • 1
    Thank you for your sharing your experience. – Airy Nov 22 '21 at 10:12

3 Answers3

7

You just need to read the official document in the deployement section: it's say:

The libev and libevent projects implement high-performance asynchronous event driven C libraries. The PHP extensions ev and event are available to interface with these libraries. They allow an application to transparently use the best kernel level evented I/O method (select, poll, epoll, kqueue, or event ports) available for the operating system it is running on.

The ev PHP extension bundles the libev C library in its source and requires no prior setup. If you want to use the event PHP extension, you need to first install the libevent library along with its headers for your operating system. For example on Debian/Ubuntu:

-$ sudo apt-get install libevent libevent-dev

You may then install the ev or the event extension, either through your preferred package manager, or directly using pecl:

-$ sudo pecl install ev

-$ sudo pecl install event

No further setup is necessary; if either of these extensions is present, the evented I/O loop toolkit used by Ratchet will automatically utilize them, which will drastically improve concurrency. here is the link: http://socketo.me/docs/deploy

Manda
  • 71
  • 1
  • 4
  • My server is Ubuntu 16.04 and already had libevent-2.0-5, maybe it's defaultly there. There was no such package as libevent. I installed both pecl extensions and added the lines to my php.ini and it seems to be working fine. – RaisinBranCrunch Jan 03 '19 at 20:24
3

Make sure libevent is working

if (function_exists('event_base_new')) {
    echo "\033[0m We can use LibEventLoop!!" . PHP_EOL;
} elseif (class_exists('libev\EventLoop', false)) {
    echo "\033[0m We can use LibEvLoop!!" . PHP_EOL;
} elseif (class_exists('EventBase', false)) {
    echo "\033[0m We can use ExtEventLoop!!" . PHP_EOL;
} 
else 
{
    echo "\033[0m We can use StreamSelectLoop!!" . PHP_EOL;
}

php-fpm.conf:

rlimit_files = 65536
rlimit_core = unlimited

OS limits

https://rtcamp.com/tutorials/linux/increase-open-files-limit/

Taken from https://github.com/ratchetphp/Ratchet/issues/376

cetver
  • 11,279
  • 5
  • 36
  • 56
1

I had a similar experience that may help someone. My server was stoping responding after one hour when the number of concurrent socket connections reached about 700. After trying all of the possible solutions, I realized that I had a ProxyPass in apache which redirects port 443 (SSL) to 8080 (my socket port) In other words from ws to wss like discussed here.

Finally, I increased the ServerLimit in my Apache prefork configuration from 500 to 1700 and the problem was solved temporarily. (You should increase MaxRequestWorkers as well)

This shows that if you use ReverseProxy in Apache (or another webserver) the Apache will become busy as it gets stuck between the client and WebSocket server.

I do not recommend increasing the Apache config for the final solution, but I want to mention considering this as a potential bottleneck. Maybe the best solution is to run a direct wss. (I anyone knows a good tutorial please comment that)

The second thing I should mention is that there is a hard limit of 1024 connections which in my case was exactly 1019 concurrent connections that will be solved by installing ev or event. I installed ev but due to a problem during the installation, it did not work well. I had to reinstall it and the problem was solved (uninstall using pecl uninstall ev). I used the following lines for installing it on php8.0:

apt install php8.0-dev
pecl install ev
sudo echo 'extension=ev.so' > /etc/php/8.0/mods-available/ev.ini

sudo phpenmod -v 8.0 ev

# Check module (it will echo ev if installation is successful):
php8.0 -i | grep -i ev
s.abbaasi
  • 952
  • 6
  • 14