1

I created real time chat with long-polling. It works as follows:

First, user sends a request to the server. The server gets the request and waits for a new message.

If new message comes, it will push a message to the user's browser (if the request closed, then browser will resend it). If the server waited more than 3 min, the request will be cancelled and new one will be sent.

On the server, I used PHP. In PHP, I just check message count after every 600 milliseconds. If it is greater than the previous count, the server will get new messages and send them to the browser as responses.

The problem is that I get max_user_connection error, even though I close mysqli connections after every SQL check in the database.

If I do not close mysqli, error happens more often.

How I can solve this problem? (For now My host is Hostgator and plan is shared)

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Alex
  • 29
  • 5

1 Answers1

0

Log into the mysql server and run this command and find out how many max connections you have set?

mysql>show variables like "max_connections";
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 100   |
+-----------------+-------+

If its less than the number of connections you are requesting at this moment, then update the number of connections.

mysql>set global max_connections = 200;

This should solve your problem. Remember max_connections is dynamic, you don't have to stop, or restart mysql server. You can directly update the number of connections.

  • 1
    Why max_connection limit exists?What is the benefit of it?Why hosts do it?is not it dangerous increase max_connection llimit? – Alex Jul 16 '16 at 18:49
  • No, its not dangerous, mysql has a default limit, you can always increase dynamically If you want to. This operation will not lock anything. But don't increase it too much. This thread might help, http://dba.stackexchange.com/questions/23498/what-should-i-set-max-connections-to-for-mysql –  Jul 16 '16 at 18:55
  • 1
    thank you for information this was useful unfortunately,I cant increase it too much,I want to learn that what is the problem with ajax long polling?When I do simple ajax polling it works perfect but banwidth increase really much when I do long polling bandwidth decrease very musc but this problem occur.What should I do? – Alex Jul 17 '16 at 06:27
  • Sorry for the delay @Alex, are you using multiple long polling? can I know the default max_connections set on this server? could you check whether this is because of persistent connections? Could you also run `show full processlist` command on mysql and see how many connections are open and what they are connected to? –  Jul 18 '16 at 17:49
  • I use 3 long polling at the same time but number of the long pollings just decrease error happened time.This error happened very rarely for example per half (maybe 1)hour but I should use this full half hour.But this became like this only I connected to server.I cant imagine if a lot of real user connected what will happen.I use simple long polling but I get this error :( every time.If I use just ajax polling I never get this error. Error say that my limit is 8646 – Alex Jul 18 '16 at 18:36
  • @Alex This probably because of persistent connections, persistent connection leaves the connections open. When you have multiplpe long polling you might end up having too many threads still open. Could you provide me the output of this query? `show variables like "max_connections";` –  Jul 18 '16 at 18:54
  • 1
    for now I have not access to my host but I know max_connection limit that I mentioned before 8646.It is persistent connection problem I think so.But should use simple polling or long polling in this case? – Alex Jul 19 '16 at 20:50
  • Hello @alex I want to suggest you, but I do not know about your system spec details. General pooling aka short polling makes sense to me since you are having persistent connections problem. Please read through these links, they will give you a better answer, 1. http://stackoverflow.com/questions/5313641/scaling-a-chat-app-short-polling-vs-long-polling-ajax-php?noredirect=1&lq=1 2. http://stackoverflow.com/questions/4642598/short-polling-vs-long-polling-for-real-time-web-applications?noredirect=1&lq=1 –  Jul 19 '16 at 21:05
  • 1
    Thank you, these ones is really helpful I understand that I need really powerful server to do this,I actually have done a lot of complex optimization but it didnt work because My server is not good enough – Alex Jul 21 '16 at 11:17