Skip to content

Instantly share code, notes, and snippets.

@paulstatezny
Created October 26, 2021 00:24
Show Gist options
  • Save paulstatezny/78ed5a39d9a7e7960992cc718a57c037 to your computer and use it in GitHub Desktop.
Save paulstatezny/78ed5a39d9a7e7960992cc718a57c037 to your computer and use it in GitHub Desktop.
How to send an event from within `handle_event` in a Phoenix LiveView
def send_event(socket, component \\ nil, event, payload) do
cid = case component do
%Phoenix.LiveComponent.CID{cid: cid} -> cid
_ -> nil
end
send(
socket.root_pid,
%Phoenix.Socket.Message{
event: "event",
join_ref: nil,
payload: %{
"cid" => cid,
"event" => event,
"type" => "other",
"value" => payload
},
ref: nil,
topic: "lv:" <> socket.id
}
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment