Suppose I have a Phoenix app that includes a realtime dashboard of data.
I have a channel called MyApp.DashboardChannel
, with a single topic of dashboard
.
In MyApp.DashboardChannel
, I have a function like:
def send_update do
MyApp.Endpoint.broadcast(
"dashboard",
"update",
%{data: MyApp.get_dashboard_data}
)
end
Whenever something happens to modify the data being displayed in the dashboard, I can call this function to push out an updated version. For instance, in MyApp.TransactionController.create
, after a transaction is saved, I can call DashboardChannel.send_update
so that everyone will see the new transaction.
Now, suppose I want to modify this so that each dashboard user gets customized data. Essentially I want to say this: "for each connected user, run a query using their user_id
and push the resulting data to them."
How can I do this?
I'm pretty sure the answer will involve Phoenix.Presence
. I've already added it as the docs show, and am successfully getting presence_state
and presence_diff
events in the browser.
However:
- I can't use
Presence.list(socket)
when callingsend_update
, because I'm calling it from a controller which has no access to asocket
- When I call
Presence.list(MyApp.DashboardChannel, "dashboard")
I get an** (EXIT) no process
fromGenServer
. Am I misunderstanding how to use this?