coen.bakker
Send message, `send(pid, message)`, from controller to a LiveView process not possible? If so, why not?
I was trying out a pattern I haven’t tried before: sending a message from a regular API controller to a LiveView process.
See code below for some of the relevant code. In a nutshell. One route for LiveView showing a registration form. One route for handling POST requests from that form. On form submit an AJAX POST request is made from the LiveView. In case there are any validation errors the form controller tries to send a message to the LiveView. At this point that message is just a non-sense test message. The LiveView does not receive the message, however.
# Router
live "/form", FormLive
post "/form", FormController, :create
# PageLive
<script>
function handleSubmit(event) {
event.preventDefault()
const form = event.srcElement
const formdata = new FormData(form)
const ajax = new XMLHttpRequest()
ajax.open("POST", "/form", true)
const crsfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
ajax.setRequestHeader('x-csrf-token', crsfToken)
ajax.send(formdata)
}
</script>
def handle_info(:hello, socket) do
IO.puts "RECEIVED" # Does not print after controller has sent the message
{:noreply, socket}
end
# RouteController
# one or several validation errors
%{"sender_pid" => sender_pid} = changeset.params
IO.inspect sender_pid # "#PID<0.2900.0>" Note: String
pid = String.slice(sender_pid, 5..-2)
pid = pid(pid) |> IO.inspect # #PID<0.2900.0>
send(pid, :hello)
conn
|> Plug.Conn.resp(406, "Not all form requirements have been met.")
|> Plug.Conn.send_resp()
I hope this code provides sufficient context for my question: It seems I can’t send messages from my controller to the LiveView, why is that? I reckon I can use PubSub to get the job done, but I prefer to understand why the pattern I’m trying doesn’t work. I looked around in docs etc., but haven’t found a definite answer, yet.
Marked As Solved
derek-zhou
You can send message from anywhere to anywhere. Most likely you didn’t have the right pid. No one knows the pid except the LV process itself and its supervisor. How did you get the info to the controller?
Also Liked
LostKobrakai
I’d suggest taking a look at trigger-action. It allows LV form handling to trigger a POST request once the form is validated, up until that everything is handled as normal using just the LV.
benwilson512
As a design question, can you elaborate on why you’re doing it this way instead of submitting the form into the live view process itself? This seems like a much more complicated way of achieving phx-submit.
axelson
FYI mix phx.gen.auth —live was announced at ElixirConf this year. I’m not sure if the code for it is available yet but that may provide some patterns that you can use here.
Last Post!
coen.bakker
Thank you for replies. I’m soaking in all the input. Appreciate all the lessons learnt during this project.
Popular in Questions
Other popular topics
Latest Phoenix Threads
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









