High performance long polling chat
Background
There is a site on Laravel with real-time traffic of 700-1000 people. Previously, the site used a third-party chat. He used WebSockets . Everything was fine, until at one point the chat developer refused to support him due to the high load. From that moment, the search began for alternative chat systems ...
Technology selection
Many will say that now no one is using old browsers like Opera12 or IE8, but there are still quite a lot of such people, so it was decided to choose Long Polling .
Initially, the site was hosted on Shared hosting , due to which there was a huge number of restrictions. However, the hosting handled the load. It pleased. And the only possible chat implementation was long polling in PHP .
Implementation
From words to deeds

The principle is clear and simple. Ajax sends a request to a server that reads messages. The duration of the request, or rather the response from the server, depends on whether someone added a message. That is, after the request, a cycle is launched that checks to see if there are new messages, and if there is a new message, the cycle is interrupted and a response is returned with a new message. After receiving a response, the request is repeated. Etc.
Implementing such a thing in PHP is not much easier. I spent on the implementation of a total of about 3 hours of my life.
Problems
However, left it in the MVP (Minimum viable product) stage . And, as it turned out, not in vain. When testing functionality on a production site, it simply crashed. The operative filled up within 1-2 minutes and extinguished the server.
Thinking a little, I decided that it was the server. And after some time the site was moved to VPS . I will say right away that the configuration is weak, and the RAM is only 1GB. However, on VPS chat lasted 3-4 minutes. Already better.
As a result, after lengthy excavations of the Internet, I realized that writing chat in PHP is pointless if the load on it is more than 10 people.
There are 2 options left:
- Put nodeJS and write chat on Web sockets
- Write a service on something else
And what do you think I chose? Of course, the second option, otherwise I would not write this article.
Since there are not so many C ++ \ C implementations of server applications, especially cross-platform ones, and especially since I am not strong in Sy, I chose Lazarus . It is good that he has such a component as FPHTTPServer and an example implementation of the application. I will also make a reservation that I do not have Linux at hand. I make all applications on Windows. I was just sure that recompiling the application on a different system is time to spit. After all, the motto also says! "Write once - compile everywhere!" . However, I succeeded when there were 2 systems on my laptop (win7 + linux mint).
Having compiled the application on win, I checked the functionality locally. Everything worked like a clock. Nevertheless, it was a simple exe-shnik and wanted something more - to write a normal service like apache . Lazarus - this is not Delphi, although it is very similar. Firstly, it is free, which automatically raises suspicions regarding the quality of the product. And secondly, ... however, as well as thirdly, and still a n-th amount - all the inconvenience is associated with this. Most of the questions on the Lazarus forum remain unanswered. This is very sad. As well as debugging an application with a pop-up error like "uncatchable error". But, fortunately, at the end of the week, I completed an analogue of the functionality that was written in PHP in 3 hours.
Satisfied as an elephant, I decided to compile the application on Linux. Having logged in as root on the server and running the apt-get-install lazarus command, I quickly installed Lazarus, copied the sources and, using the lazbuild -r project.lpi commands previously discovered on the Internet, compiled the application. It was the only, truly, the most successful and fastest stage.
After checking the work of the chat already at a real load, I realized that my idea was justified. Chat worked, server load did not increase much. It pleased. Now I could return to Windows and continue working on an improved version - a service (daemon). As expected - everything worked on Windows, albeit with a slight delay in time.

On Linux, the project did not compile. When compiling, an error was issued stating that the Interfaces unit was not found . It was very strange. At the Lazarus forum, a clear answer did not appear. Many only wrote that when reinstalling it worked. However, reinstallation did not help. In the end, I realized that my version of Lazarus is different from the one on Linux. Update repository failed. More precisely, the repository has been updated, but Lazarus has remained the same version.
Then I tried to download the installation package using wget. As a result, error 177 got out (I don’t remember the exact number anymore). After another splash of emotions, I realized that the problem is in some kind of symbol in the link. I uploaded the installation package to MS OneDrive and created a short link, after which I successfully downloaded the file.
Next, I deleted the old lazarus and installed a new version. And what do you think the compiler said? Of course! Incomprehensible mistakes!
Based on past bitter experience with finding answers on the Internet, I began to study Lazarus configs. The problem turned out to be simple - for some reason, during installation, Lazarus did not create a new folder with configs. Daddy is left over from the previous version, although I removed the old lazarus with apt-get remove --purge. Well, I saved the old configs, deleted the folder, and again tried to recompile the Lazarus IDE. Magically, after that the config folder was created and I was able to finally compile a daemon for Linux.
Victory! Or not?
The service application has compiled successfully, but how can it be launched now? As you would expect, a simple script to create an entry in service start ... did not work. Moreover, I again found forum posts with my unanswered problem.
After digging another hour of the Internet, I found how you can start the service using start-stop-daemon . In the end, I realized that the code for the startup script that was posted in the Lazarus examples works, but only if you run it from the terminal. Otherwise, it would produce something like "some file was not found."
Finish
The problem with starting / stopping the daimon through service I still have not solved. Nevertheless, I was pleased with the work done. Moreover, the chat was able to withstand 1000 connections for 4 hours. Unfortunately, the server configuration did not allow it to work longer. However, I solved this problem using the "show chat" button. As a result, the load on the chat was given only by users who really need it.