How to connect users? (Like Omegle.com)

I am trying to make a clone of the Omegle.com script, mainly for training purposes. I am doing this in PHP / MySQL / AJAX.

I have problems finding two users and connecting them. The purpose of omegle is to connect two users “randomly”.

Now I am doing the following:

  • When a user logs on to the site, a session is assigned.
  • There are 3 states for each session / user (Normal, Standby, Chat)
  • First, the user has the Normal state and the field is connected_to = NULL
  • If the user presses the “START” button, the status is “Standby”. Then he searches for another user with the Waiting state, if he does not find him, then he will continue the cycle, waiting for the change to "connected_to". Connected_to will change when another user clicks the START button, and then finds another user waiting and updates the sessions accordingly.

Now you have a few problems, for example:

  • A user can only connect to one user at a time. In omegle, you can open several chats at the same time.
  • I don't know if this is the best.

About the chat, each user polling events from the server using AJAX calls, I saw that omegle, and not several HTTP requests every second (say), makes ONE request and waits for a response, this means that the PHP script loops until it receives a response . I did this with set_time_limit (30) every time the loop started. Then when the Ajax call completes again. Is this approach right?

I would be grateful for your answers, Thank you,

Carlos

+4
source share
1 answer

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.

+1
source

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


All Articles