johnmcguin

johnmcguin

hook's handleEvent event not working

Context: I have a game that is client rendered via a live view with a custom hook. I am looking to persist a value to local storage so that the client side can re-hydrate its state on reconnect or refresh.

My live view gets set up via handle_params/3 and looks like

def handle_params(%{"id" => id, "name" => name}, _url, socket) do
  case Games.get_active_room(id) do
    nil ->
      socket
      |> put_flash(:error, "Game room #{id} not found")
      |> push_navigate(to: "/")
      |> then(fn socket -> {:noreply, socket} end)

    %Room{} = room ->
      Endpoint.subscribe(@current_game <> id)
      Presence.track(self(), @current_game <> id, name, %{name: name})
      Presence.track(self(), @all_players, "game_" <> id <> "_" <> name, %{name: name})
      broadcast_readiness(@current_game <> id, game_ready?(id, room.num_players))

      socket
      |> push_event("set_game_id", %{id: nil})
      |> assign(name: name)
      |> assign(room_id: id)
      |> assign(messages: %{})
      |> assign(game_ready: false)
      |> assign(winner: "")
      |> assign_async(:answer, fn -> get_answer(id) end)
      |> then(fn socket -> {:noreply, socket} end)
  end
end

In the hook’s mounted callback, I have the following listener (which does not work):

this.handleEvent("set_game_id", function (game) {
  console.log("set_game_id called");
  console.log({ game });
});

However, after doing a little debugging, I realized that registering an event listener on the window does work:

window.addEventListener("phx:set_game_id", (e) => console.log(e));

Is there something I am missing with regards to getting this to work in the hook?

First Post!

cmo

cmo

That sort of thing does work. I usually only push events in the connected mount/handle_params though. You could try if connected?(socket), do: push_event(...)

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement