mortenlund
LiveView - Live Component - unmount callback
Hi!
I would like to suggest a new callback in the lifecycle of the Live Component which is unmount.
Sometimes it is nice to be able to cleanup subscriptions and such when the component is removed from the page.
An example is a “chat” component I made that subscribes to PubSub.
The component is used in a page with tabs and is only present in one of the tabs.
Being able to unsubscribe from the PubSub in the same way the subscription was made in mount would be very nice.
First Post!
D4no0
I think you should be able to use this terminate callback, even though it’s not guaranteed that it will get called (read why in the docs).
The official way to do this is by monitoring the process: Process — Elixir v1.20.2
Most Liked
LostKobrakai
How would you decide if a message received for being subscribed to pubsub is meant to be forwarded to the live component? Those messages are in arbitrary shape and don’t include any hint if the source for the subscription might be a child live component or the live view itself. It could even be that both are subscribed to the same pubsub.
LostKobrakai
Tbh I think the issue here is that you use a LiveComponent to subscribe to PubSub in the first place. Given LiveComponents are indeed not processes you’re essentially leaking a subscription onto the parent LiveView as a sideeffect, which then materializes by the fact that you cannot clean that up anymore. I agree that this is a limitation to what a LiveComponent can encapsulate, but imo the solution isn’t an unmount callback. Even with that there would still be no handle_info callback as well, so you’re still dependant on the parent LV.
mortenlund
I very much agree on this!
I have another suggestion to allow PubSub to send events directly to the LiveComponent since this is the same way LiveComponent allready gets its message when using send_update.
Then the LiveComponent could be able to use PubSub without involving the parent LiveView.
This could then call update/2 in the same way send_update allready does this ![]()
The way I solve the handle_info in the parent today is to use Phoenix.LiveView.attach_hook/4 function and then use send_update to forward it to the component ![]()
I attach the hook by defining a hook/1 function in my component that is called from the LiveView.mount function ![]()
Last Post!
mortenlund
Well, they can with some slight tricks
![]()
I currently have a special module like this that handles the subscription and acts as a special dispatcher.
This does not rely on the parent LiveView and the subscription will be cleaned up when the parent LiveView process dies as normal PubSub subscriptions ![]()
defmodule MyLiveComponent.PubSub do
@moduledoc false
def subscribe(%Phoenix.LiveView.Socket{assigns: %{myself: myself} = socket, pubsub, topic) do
if Phoenix.LiveView.connected?(socket) do
Phoenix.PubSub.subscribe(pubsub, topic, metadata: %{pid: self(), cid: myself})
socket
else
socket
end
end
def subscribe(%Phoenix.LiveView.Socket{} = socket, pubsub, topic) do
Phoenix.PubSub.subscribe(pubsub, topic)
socket
end
def unsubscribe(pubsub, topic) do
Phoenix.PubSub.unsubscribe(pubsub, topic)
end
def broadcast(pubsub, topic, message) do
Phoenix.PubSub.broadcast(
pubsub,
topic,
message,
__MODULE__
)
end
def broadcast_from(pubsub, pid, topic, message) do
Phoenix.PubSub.broadcast_from(
pubsub,
pid,
topic,
message,
__MODULE__
)
end
def local_broadcast(pubsub, topic, message) do
Phoenix.PubSub.local_broadcast(
pubsub,
topic,
message,
__MODULE__
)
end
def dispatch(entries, _dispatch_identificator, message) do
entries
|> Enum.each(fn
{_pid, %{cid: cid, pid: pid}} ->
Phoenix.LiveView.send_update(pid, cid, source: :pubsub, message: message)
{pid, _} ->
send(pid, message)
end)
:ok
end
end
And a live component can be something like this:
defmodule MyComponent do
use MyWeb, :live_component
import MyWeb.CoreComponents
@impl true
def render(assigns) do
~H"""
<%= @message %>
<.button phx-event="send">Send</.button>
"""
end
@impl true
def update(%{source: :pubsub, message: message}, socket) do
socket
|> assign(:message, message)
end
@impl true
def mount(socket) do
updated_socket = socket
|> assign(:message, "No message")
|> MyLiveComponent.PubSub.subscribe(MyPubSub, "topic")
{:ok, updated_socket}
end
@impl true
def handle_event("send", _params, socket) do
MyLiveComponent.PubSub.broadcast(MyPubSub, "topic", "We got a message!")
{noreply, socket}
end
Popular in Proposals: Ideas
Other popular topics
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
- #api
- #forms
- #metaprogramming
- #security
- #hex









