silverdr
I know I can have heex attributes like phx-click="eventid" in the markup and then
def handle_event("eventid", _, socket), do: [...]
But how does one send that event without actually interacting? There are some examples in the guides like:
yet they seem to cover different cases:
- handle custom client-side JavaScript when an element is added, updated, or removed by the server
- Triggering
phx-form events with JavaScript
but what I need is best described as “triggering arbitrary event that can be handled with handle_event/3 with JavaScript”. I found something similar here:
yet it’s been some time and many Phoenix changes since that fine piece was written.
How would you approach this task today?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cblavier
You can send arbitrary events to your component from JS by using
pushEvent: JavaScript interoperability — Phoenix LiveView v1.2.5You just need to attach a Hook to your component and call
pushEventfrom there.silverdr
The hooks part of the guide says: “To handle custom client-side JavaScript when an element is added, updated, or removed by the server […]”. In my case there is no user interaction, no server “interaction”, nothing is mounted, added/removed nor updated. It’s just a JS callback function, which gets called by another piece of JS code. I’d like that JS callback to notify backend about the fact that it was called.
cblavier
You need a hook attached to the live_component that will receive the events. This hook will be initialized on mount.
Then in your hook, listen for custom JS events and propagate them to your LiveComponent.
You can now trigger JS events from wherever you want, they will be handled by the hook and propagated to your LiveComponent
I just wrote this code directly on the forum, I hope this is kind of working
LostKobrakai
One can also look at @cblavier’s answer from the bottom up.
You have something happening somewhere in your JS (your callback) and want it to notify a LV or LiveComponent. How would you tell whatever API you call where things should be routed to? There’s no module names or fixed IDs you could hardcode there. Your LV or component might even be present multiple times on the current page. So your best best is some form of PubSub, where producers of data don’t need to know consumers upfront. That’s the
dispatchEventpart.Now how do you consume those dispatched events in a way that the can be forwarded to your LV pieces/server. That’s where hooks come in. They connect some JS with a section of markup, which LV knows how to connect to things on the server. Hooks do receive LV lifecycle events that’s true, but that doesn’t mean all they do needs to be based on lifecycle events. You can also listen for any other things happening and react to those as well. That’s the
addEventListenerpart.So now the hook receives events. Then you can use
pushEventto push data forward to the server to handle in anhandle_eventcallback.silverdr
Other than two small typos (missing set of empty parens and “addEventListeRner”) it does
thank you!
Yes, that’s a very good explanation, thank you!
What I think I was hoping for was something like for example
phx-js="eventid"(form/syntax aside) and the rest would be already set, similar to how e. g.phx-click=and Co. is working but the way you explained it is fine too. I should probably push a PR updating the docs so that it is clear that not only “form events” and “lifecycle events” are effortlessly handled this way.chrisdel101
Sorry to necropost but I’m stuck w this issue. Both of these issues occur for me. I can receive from the server (on the client), but cannot push to the server.
unknown hook found for..confHookLiveSocket.pushEvent is not a functionsodapopcan
What does your
app.jslook like? Having only those errors as clues, they lead me to believe that you haven’t passed your hooks into yourliveSocketconstructor.This isn’t done by default by
mix phx.new, you have to do it manually.If you’ve done this already then sorry!
chrisdel101
Well I’ve deleted and discarded the non-working code now. But yeah, I was passing in the hooks like that.