Search code examples
database-connectionchatcometlong-pollingserver-sent-events

max_user_connection error while real time chat


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)


Solution

  • 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.