Answer the question
In order to leave comments, you need to log in
How it is better to organize data exchange on sockets between users?
Good afternoon. I will not describe the real situation, because. it is rather difficult, I will try to tell the essence that I want to achieve and actually hear your opinions on this issue.
So, imagine the situation: there is a game in which players connect to a room and wait for it to start. When the game starts (it doesn't matter how it starts, it just started), each player will know about it via broadcast. Everything is good here. Next, each player starts a timer with an interval of 1 second (roughly speaking, the duration of the current game). Every second, each player generates a number (in our general case, let it be some random value) and he needs to send this number to all the participants in this room, and they, in turn, need to receive this number at the same second. The server side is written in Node.js and Socket.io.
For example:
1) there is player1 and player2
2) the game started
3) the 1st second of the game: player1 sent the number 15 to the server, and player 2 - the number 18
4) in the same 1st second, player1 should see that player2 sent the number 18, and player2 should see that player1 sent the number 15
The problem is that I'm not sure how to implement this correctly, I don’t know if the data will have time to reach the players in this 1st second, because they need to send their number, and everyone else will receive this number in the same second. I see 2 options:
1) Player1 sends his number to the server. We put this number into an array, something like this: usersPoints[userId][second] = points. At the same second, player2 requests data from the array and is already looking at how many points player1 has. Problem:I'm not sure that the data from player1 will have time to rely on the array before player2 requests it.
2) Player1 sends his number to the server. The server broadcasts this number to the room. Problem: I'm not sure that the number from player1 will reach the server exactly at the second when player2 needs it. Well, let's take slow internet as an example. It turns out that according to the timer for player1, the event "send a number to the server" will work. Player2 at the same second will wait for a number from player1, but since player1 has slow internet, and player2's timer will not wait for it, then player2 may simply not wait for this number.
I tested the 1st option and the result was expected (by me), the number either reached (to all the others), or not. I tried to send points not every second, but every 990ms, and request every second, but then over time (already after 2 minutes) they no longer become completely relevant.
If you don't quite understand why players send and receive numbers, then let's try to explain in general. When the game starts every second, each player generates a number of points by their actions. The rest of the players need to see his points at that second (and he sees the points of other players) in order to compare them with his own.
I'm desperate. It seems that the problem is quite common, the exchange of relevant data in the current second, but I don’t know which approach to choose. Hence the question: what is the best way to organize the exchange of relevant data between players within one second?
Answer the question
In order to leave comments, you need to log in
it is not clear why to request them, the message in socket.io must be sent from the server, and the connection is permanent.
Design the game in such a way that the requirements for the frequency of traffic exchange between players do not exceed a reasonable ping.
Many projects died when the game was critical to ping, and because of the popularity and influx of players, it became impossible to play (the same tetrisarena, where an exchange between only two participants was required up to several times per second, began to bend when the general online on the server exceeded several thousand people), apparently they did not have time to separate the players to several different servers, and they scored on the project.
At the same time, the average ping that you should count on is up to 20 ms. ideally up to 10 ms. So calculate what and how many times you can transfer for such a period, multiply by the number of players and compare with the uplink to your server, how many players it will pull.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question