I have a Phoenix app where I want to persist a chat message from the client to the database before sending it to a channel.
In my page controller I have something like this to pick up the event from the client:
def handle_event("reply", %{"reply" => params}, socket) do
changeset = Reply.changeset(%Reply{}, params )
case Repo.insert(changeset) do
..
..
end
Phoenix.Channel.push(socket, "new_reply", %{text: "this is a reply", guid: "123" })
{:noreply, socket}
The problem is that the push/3 call always fails with the following error:
(FunctionClauseError) no function clause matching in Phoenix.Channel.assert_joined!/1
(phoenix 1.6.10) lib/phoenix/channel.ex:639: Phoenix.Channel.assert_joined!(
..
)
(phoenix 1.6.10) lib/phoenix/channel.ex:582: Phoenix.Channel.push/3
(chat 0.1.0) lib/chat_web/controllers/page_controller.ex:259: ChatWeb.MainPage.handle_event/3
(phoenix_live_view 0.17.10) lib/phoenix_live_view/channel.ex:382: anonymous fn/3 in Phoenix.LiveView.Channel.view_handle_event/3
(telemetry 1.1.0) /../deps/telemetry/src/telemetry.erl:320: :telemetry.span/3
(phoenix_live_view 0.17.10) lib/phoenix_live_view/channel.ex:216: Phoenix.LiveView.Channel.handle_info/2
(stdlib 3.17.2) gen_server.erl:695: :gen_server.try_dispatch/4
(stdlib 3.17.2) gen_server.erl:771: :gen_server.handle_msg/6
(stdlib 3.17.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Can anybody tell me what I’m doing wrong? Is there some reason why a page controller can’t push a message to a channel?
Thanks!