willc0de4food
Howdy,
I’m struggling with something that seems like it should be simple. I would like to fetch some data via async when the page loads, and then once loaded, graph that data with Highcharts. Everything I’ve read has indicated that you need to setup a hook that will allow you to pass the data from the server to the JS, and you use a handle_event to trigger push_event() with the data. I don’t have a user interaction to trigger the handle_event, I just want the graph to load once the data is ready. I tried adding the push_event() to handle_async , but nothing happens in the JS hook. I have added a dbg() line to verify that the data is being received in the handle_async.
What am I missing? ![]()
Some code:
def mount() do
...
|> start_async(:points, fn -> Tokens.generate_chart_data() end)
...
end
def handle_async(:points, {:ok, points}, socket) do
dbg(points)
{:noreply, push_event(socket, :points, %{points: points})}
end
def render(assigns) do
~H"""
...
<div class="mt-4 flex flex-row w-full">
<div id="graph" phx-hook="Graph"></div>
</div>
"""
end
var Hooks = {};
Hooks.Graph = {
mounted() {
logToConsole('mounted');
},
updated() {
logToConsole('updated');
this.handleEvent("points", ({points}) => logToConsole(points);
}
}
let logToConsole = (action) => {
console.log('*********************', action);
}
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cmo
Firstly, you need to put
this.handleEvent("points", ({points}) => logToConsole(points);inmounted.You can set it up so that the process receives a message every
xseconds, which you handle inhandle_infoand can trigger thestart_asyncfrom there.You can send the message every x seconds with
:erlang.send_interval(Time, Destination, Message)or trigger it each time you process the message inhandle_info/handle_async. That isProcess.send_after(...)), called once inmountand then again inhandle_infoIf the data is on some publication you could subscribe to that in your liveview.
willc0de4food
Hello! Thanks for that info. Do I need
this.handleEventin bothmounted()andupdated()? Or just inmounted()?The data is static, on my local server. Basically just a bunch of numbers and timestamps that are retrieved, compiled and sent to the front end. So once the data has been retrieved, it won’t change.
Sorry, I’m new to Elixir & Phoenix, where in the liveview would I setup the
:erlang.send_interval()?cmo
:erlang.send_intervalwould go inmountthis.handleEventadds an event handler, so you only need to it inmounted().If the data is quick to retrieve, you can do the work in
handle_infoand not worry about the async retrieval.willc0de4food
Update: I got it working without hooks! It was as straight forward as it seemed like it should be, I just didn’t know what I didn’t know. The final working product:
live view:
app.js:
That’s it
cmo
I’d still go with a hook as often you want to dispose of the chart in
destroyedand/or modify it inupdateand push the event to the specific chart.So sorry for the timer stuff, I had realtime chart on the brain from a post on the discord and conflated the two.
willc0de4food
Ok, that makes more sense w/the timer stuff. I was wondering, but figured I’d give it a try just to see if I could get ANYTHING to work

I’m still unclear as to how I would implement this with hooks as there’s no user interaction
cmo
To do the initial one, you push an event in
mountif it is a liveview or inupdateif it is a live component.For example:
In a liveview that uses this, I push “loading” on mount and when params are changed, I push “loading” again and call
start_asyncto fetch the new data async. When it lands, I push an “update” event with the new data. Echarts has a built in show/hide loading indicator function which I’m using. You could a similar thing with the<.async_result>component.