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(...)

Last Post!

johnmcguin

johnmcguin

Thanks, I may try that just to learn. I ended up just changing the implementation to pass these as attributes on the element via assigns, which feels much simpler for the use case.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

We're in Beta

About us Mission Statement