Hello,
We have been using Elixir/Phoenix for our production app. We have noticed one issue with the way the messages are sent to clients connected through channels.
Our app is a online poker game. Multiple players can connect to one room. They do some actions during the gameplay. Their actions need to be broadcasted to all others players in the room. Some players can leave in between and some can stay for longer time.
This works most of the time. After some time, the players are not getting the messages in time.
For e.g. There are 4 players in a room. If the messages that are to be sent are: “msg1”, “msg2”, “msg3” and “msg4” in the given order. 2 Players are receiving them in the correct time. They are getting “msg1” first, then “msg2”, then “msg3” and then “msg4”. But other 2 players are receiving them after the first 2 players got all 4 messages.
Time 00:00:00: Player1, Player2 receive "msg1"
Time 00:00:01: Player1, Player2 receive "msg2"
Time 00:00:02: Player1, Player2 receive "msg3"
Time 00:00:03: Player1, Player2 receive "msg4"
Time 00:00:04: Player3, Player4 receive "msg1"
Time 00:00:05: Player3, Player4 receive "msg2"
Time 00:00:06: Player3, Player4 receive "msg3"
Time 00:00:07: Player3, Player4 receive "msg4"
The order of receiving the messages are fine. But, the time they receive is not correct. And, this results in an incorrect state in the game.
Can you please help us. The pseudo code we have is :
Code for broadcasting game state:
Webapp.Endpoint.broadcast(room_id, "update:gameplay_state", gameplay_state)
Channel code:
intercept ["update:gameplay_state"]
def handle_out("update:gameplay_state", gameplay_state, socket) do
Channel.push socket, "update:state", %{game_play_state: gameplay_state}
{:noreply, socket}
end
Thanks,
-Ravi