Multiplayer game - Elixir channels

Nuno_Coletiv Source

I'm beginner in Elixir.

I have an elixir application for a multiplayer game that simply replicates the received command to all players connected to the channel. This is work but have some latency when replicates the received command. what is the best option for remove latency problem?

For replicates the command to all players connected to the channel, i use the broadcast function. Is the best function for this?

See follow code:

defmodule GameWeb.GameChannel do
  use GameWeb, :channel

  alias Game.GameState
  alias Game.Error

  # join to topic game:*
  def join("game:" <> code, %{"email" => email}, socket) do
    case Map.has_key?(GameState.games(), code) do
      true ->
        socket = assign(socket, :player, 2)

        game =
          code
          |> GameState.get_game()
          |> Map.put(:player2, %{:email => email, :score => 0})
          |> GameState.update_game()

        socket = assign(socket, :game, game)
        {:ok, game, socket}

      false ->
        socket = assign(socket, :player, 1)

        game =
          GameState.create_game(code)
          |> Map.put(:player1, %{:email => email, :score => 0})
          |> GameState.update_game()

        socket = assign(socket, :game, game)
        {:ok, game, socket}
    end
  end

  # topic not found
  def join(_topic, _payload, _socket) do
    {:error, Error.get(:resource_not_found)}
  end

  def handle_in("playerAction", payload, socket) do
    broadcast!(socket, "playerAction", Map.put(payload, :from_player, socket.assigns.player))
    {:noreply, socket}
  end

end
websocketelixirotpphoenix-channels

Answers

comments powered by Disqus