I personally do not see a big difference between polling the server and leaving the request open indefinitely, since both of them have obvious pros and cons. Try both and see which one costs more. If the server is busy processing several clients, this leads to timeout errors for new visitors, then this is not a good situation for a chat room, such as yours.
You may want to look at the comet server or even at the web sockets as soon as you can do something else, but I would focus on the chat / correspondence functions to work first.
So, if a user can have several chats, but they are still one on one in a chat, I will personally think about a solution where, if a user wants a new chat (if he is voluntary), they will actually start a new session. Make sure that each user object can have several (or endless) chat sessions, each of which, as you described: "normal, waiting, chatting". For user A to enter the room, he clicks the "Finish" button, and this changes the initial session from normal to waiting. When he meets someone, a new “normal” session begins, and he can change it to “wait” by clicking on a button, etc.
One thing that can lead to a constant cycle is simply asking for the number of available or “waiting” users. If it is 0, keep checking until it is above 0, instead of scrolling through all the members in the room to see if they are waiting or not.
In the second thought, instead of 0, make it more than 1, because if you have a method that calculates the total, the expecting guy will count.
source share