I have a socket with validation:
defmodule Test.UserSocket do use Phoenix.Socket ## Channels channel "user:*", Test.RoomChannel def connect(_params, socket) do case Phoenix.Token.verify(socket, "user", _params["token"]) do {:ok, uid} -> {:ok, assign(socket, :user_id, uid)} {:error, _} -> :error end end def id(_socket), do: "user:#{_socket.assigns.user_id}" end
And after connecting the socket named as user:#id
From the documentation, I can send the disconnection event Test.Endpoint.broadcast("users_socket:" <> user.id, "disconnect", %{})
Question: How to send a custom event to a socket using user:#id , it should be like a push notification for a specific socket.
I tried Test.Endpoint.broadcast "user:1", "new:msg", %{user: "SYSTEM", body: "iex"} , but it does not work because I can not listen to "new: msg" in the socket.
elixir phoenix-framework
user1156168
source share