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
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(...)
Popular in Questions
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
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
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
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
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
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
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
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
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
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
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
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









