Hey guys first post here!
It seems that I’m missing something because I can’t get this problem to work.
I’m building a game matchmaking were for example two people want to play and I generate one ID for them to join a channel.
I made all clients join with differents tokens, and a worker sends it down the rabbithole
def join("search:" <> token, message, socket) do
pid = Worker.search(%{:size => 2, :client => token})
{:ok, %{:pid => :wait}, socket}
end
In the end i have a list of tokens in a list called clients and I iterate over it to broadcast to their channels
def handle_call({:join, client}, _from, %{size: size, clients: clients} = state) do
state = %{state | clients: [client|clients]}
if length(clients) == (size - 1) do
lobby_token = :crypto.strong_rand_bytes(40) |> Base.url_encode64 |> binary_part(0, 40)
%{size: size, clients: clients} = state
broadcast(clients, lobby_token)
{:reply, {:ok, self()}, state}
else
{:reply, {:error, :full}, state}
end
end
defp broadcast([client | clients], lobby_token, count) do
BackendWeb.Endpoint.broadcast("search:#{client}", client, %{token: lobby_token})
broadcast(clients, lobby_token, count)
end
defp broadcast([], lobby_token, count), do: nil
The objective is to call again the channel for the client and send them the lobby_token that is goin to be another phoenix channel.
This handle_out lives inside search:* channel
def handle_out(event, payload, socket) do
push socket, event, payload
{:noreply, socket}
end
Finally the Js part
this.channel = this.socketService.connectTo(`search:${this.token}`);
this.channel.on(this.token, payload => {
console.log(payload);
})
this.channel.join().receive("ok", response => {
console.log(response);
//this.channel.off();
//this.connectLobby(response);
});
HERE is the problem, only the client who joins first gets the token, the first broadcast seems to work, but the second one does nothing. In my browser in one client I get the token and the other is still waiting…
I wasted like 10 Hours trying to figurate this out doing different things but I dont know how to continue…
help?¿?¿