📜 ⬆️ ⬇️

High-performance long polling chat

Prehistory


image There is a site on Laravel with real-time attendance of 700-1000 people. Previously, the site used third-party chat. He used WebSockets .

Everything was fine, until one day the chat developer refused to support him due to the high load. From that moment began the search for alternative chat systems ...

Technology selection

Many will say that now no one uses old browsers such as Opera12 or IE8, however, there are still quite a few such people, so it was decided to choose Long Polling .

Initially, the site was hosted at Shared hosting , which was why there was a huge number of restrictions. However, hosting managed the load. It pleased. And the only possible implementation of the chat was long polling in PHP .
')

Implementation


From words to deeds

image


The principle is clear and simple. Ajax sends a request to the server, which reads the message. 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 started, which checks whether there are any new messages, and if there is a new message - the cycle is interrupted, the response is returned with a new message. After receiving the response, the request is repeated. And so on.

Implementing such a thing in PHP is not much easier. I spent on implementation in the amount of about 3 hours of my life.

Problems

However, he left it in the MVP (Minimum viable product) stage. And, as it turned out, for good reason. When testing functionality on the production site simply fell. RAM was filled in for 1-2 minutes and extinguished the server.

Thinking a little, I decided that the matter is in the server. And after some time the site was transferred to the VPS . I have to say that the configuration is weak, and the RAM is only 1GB. Nevertheless, the VPS chat held for 3-4 minutes. Already better.

In the end, after a long excavation of the Internet, I realized that writing a chat in PHP is pointless if the load on it will be more than 10 people.

There are 2 options left:

  1. Set nodeJS and write chat on web sockets
  2. Write service on something else

And what do you think I chose? Of course, the second option, otherwise I would not have written this article.

image Since C ++ \ C implementations of server applications are not so many, all the more cross-platform, and all the more so since I am not strong in Ax, I chose Lazarus . It is good that it has such a component as FPHTTPServer and an example application implementation.
I’ll also make a reservation that I don’t have Linux at hand. All applications I do on Windows. I was just sure that recompiling the application on another system is to spit. After all, the same motto says! "Write once - compile everywhere!" . However, I managed it when there were 2 systems on my laptop (win7 + linux mint).

Having compiled the application on win, I checked the functions locally. Everything worked like a clock. Nevertheless, it was a simple exe-schnick and I wanted something more - to write a normal service like apache . Lazarus is not for you Delphi, although it is very similar. Firstly, it is free, which automatically raises suspicion about the quality of the product. And secondly, ... however, as well as thirdly and still the n-th number - all the inconveniences are 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 weekly period - 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. Logged in as root to the server and executing the apt-get-install lazarus command — I quickly installed Lazarus, copied the sources, and, using the lazbuild -r project.lpi commands I’ve discovered earlier on the Internet, I compiled the application. It was the only, truly, most successful and fast stage.

Having checked the work of the chat already on a real load, I realized that my idea was justified. Chat worked, server load did not increase much. It pleased. Now I could go back to Windows and continue working on an improved version of the service (daemon). As expected, everything worked out on Windows, albeit with a slight time delay.

image


On Linux, the project did not compile. When compiling, an error was issued stating that Interfaces unit was not found. It was very strange. At the forum, Lazarus did not receive a clear answer. Many only wrote that when reinstalling everything worked. However, reinstallation did not help. In the end, I realized that my version of Lazarus is different from the one that stands on Linux. Update the repository failed. More precisely, the repository has been updated, but Lazarus remained the same version.

Then I tried to download the installation package using wget. As a result, error 177 came out (I don’t remember the exact number). After the next outburst of emotions, I realized that the problem is in some symbol in the link. I downloaded the installation package on MS OneDrive and created a short link, after which I successfully downloaded the file.

Next, I deleted the old Lazarus and put a new version. And what do you think the compiler reported? Of course! Incomprehensible mistakes!

Relying on the past bitter experience with searching for answers on the Internet, I began to study the configs of lazarus. The problem turned out to be simple - when installing, Lazarus for some reason did not create a new folder with configs. Daddy remained from the previous version, although I deleted the old Lazarus using apt-get remove --purge. Well, I saved the old configs, deleted the folder and again tried to recompile Lazarus IDE. Magically, after that the configs folder was created and I was finally able to compile the daimon for linux.

Victory! Or not?

image The service application was successfully compiled, but how to start it now?

As one would expect, a simple script to create an entry in the service start ... did not work. Moreover, I again found posts on the forum with my problem without an answer.

Picked up another hour of internet, I found how to start the service using start-stop-daemon . In the end, I realized that the startup script code that was laid out in the Lazarus examples works, but only if it is launched from the terminal. Otherwise, he gave out something like "some file was not found."

The finish

The problem with starting / stopping the daimon through the service I have not decided. Nevertheless, I was satisfied 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 with the help of the “show chat” button. As a result, the load on the chat was given only by those users who really need it.

Source: https://habr.com/ru/post/275671/


All Articles