Created
October 26, 2021 00:24
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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