How can I simulate a peer-to-peer communication channel using PHP, MySQL DB and JavaScript?

The problem I am facing is to simulate a communication channel between two users of a website (for example, a gaming website), using exclusively the technologies mentioned in the title.

I recently developed an online chess site where an idea that was supposed to give users the opportunity to play live matches too, and not just long games where you would make a move, and then return in 1-15 days to find out, answered whether your adversary. And the way this game engine works involves sending asynchronous requests to the server, both to update information related to the game (in case you make a move), and to check that something has changed (if you expect the enemy to move )

To better explain this ... to the player whose move he (the browser, of course) sends an asynchronous call. request updated information about the game, exactly when it makes its move. Meanwhile, the adversary sends PERIODIC requests, β€œasking” if something has changed. If something changes, the roles switch after updating the board.

Timers are behind the functionality of the engine, so my question for you is this: how are you going to simulate a com channel between two players, trying not to put too much voltage on the server, but also having the game updated as quickly as possible to maintain a β€œlive” feeling. This is most important in 1-minute games (one of the available categories). Because in this case, the requests NEED to go through VERY often (at least 1 second). But server responses may be delayed, there will be a lot of stress on the server when hundreds of games, etc. will be available at the same time, so you see my problem.

I look forward to hearing from you guys and picking up your brain if you have any good ideas :)

Hooray! Andrew

PS: If you want to try, the name is e-chess960.com

+4
source share
2 answers

you want to create a socket server. node.js would be a good javascript based library for use on a socket server. each client will create a socket connection with the socket server, and then when the client sends a message to the socket server, the socket server can immediately send the message back to its subscribers, without storing it anywhere.

Socket servers

require access to a socket, which usually requires vps instead of a shared server.

in order to make the solution work, you need clients to be able to create socket connections to your server. some browsers may already be part of html5, but not all. You can also use Flash to create a socket connection.

+3
source

What you really want does not pull the server every second, but instead keeps the connection open and uses the observer pattern to distribute updates to specific clients (in other words: push instead of pull). Thus, your server resources will not be loaded with a new connection for every second and every client. The library for this is the comets library. Check out this good tutorial for an example application.

-2
source

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


All Articles