#Correct way to handle data fetching in live view

9 messages · Page 1 of 1 (latest)

viscid harbor
#

I have a liveview and I'm wanting to fetch some data from a remote http endpoint.

When I add the request to the mount function it appears to block the view and the websocket connection drops while loading the page. Obviously I'm doing it wrong.

This is an example of what I'm doing which is obviously incorrect

def mount(_params, _session, socket) do
  data = fetch_data_from_external_service()
  {:ok, assign(socket, :data, data)}
end

What's the correct way to do this in Phoenix? Is there a good practice example (or example project) I can follow?

broken geode
viscid harbor
#

perfect that's what I was looking fort

#

thank you very much 🙂

#

so it's ok to keep the http request in the mount function as long as its async?

rapid anvil
#

it runs in a separate process if you use async assign functions

#

and you handle the message it sends back to you