you can also do
# parent LiveView
def handle_info({:new_card, card_id}, socket) do
send_update CardStackComponent, id: :card_stack, card_id: card_id
{:noreply, socket}
end
#component
def update(%{card_id: card_id} = _assigns, socket) do
card = get_card(card_id)
socket =
update(socket, :cards, fn cards ->
[card | cards]
end)
{:ok, socket}
end
you have to have in mind that is going to be called on each render. More info about this hereupdate/2
Is also worth mention that you may want to maintain the state either in the LiveView or the LiveComponent but not both, as is mentioned here
Actually it seems that you might pass to each new connection in in your connect this particular PID or name of the Agent and it should solve this case. You only need one Agent, so you can name it, eg.user_socket
Agent.start_link(your_func, name: :myagent)
and later get from it
Agent.get(:myagent, your_get_func)
Also you can consider to use Registry, which is from Elixir 1.4 in standard library.
This agent can be in and you can just add it in the lib file as a single worker and supervise it with dedicated Supervisor if needed.<app_name>.ex