Hello everyone
I have websockets authenticated with Phoenix.Token:
def connect(%{"token" => token}, socket) do
case Phoenix.Token.verify(socket, "user_id", token, max_age: @two_weeks) do
{:ok, user_id} -> {:ok, assign(socket, :user, user_id)}
{:error, reason} -> :error
end
end
and my socket module exposes function `id`
def id(socket), do: "users_socket:#{user_id(socket)}"
def user_id(socket), do: socket.assigns[:user]
which, as far as I understand, makes it possible to register all sockets in the PubSub server with a topic like "users_socket:42".
This allows me to run
Foo.Endpoint.broadcast("users_socket:42", "disconnect", %{})
which sends a disconnect message to all sockets of the user with ID 42. It does work.
But how do I send a channel message to all windows of that user? I need a way to send messages to browser windows on some server side event.
All my attempts such as the following
pubsub_server = Foo.Endpoint.__pubsub_server__
Phoenix.PubSub.broadcast! pubsub_server, "users_socket:42", %Phoenix.Socket.Broadcast{
topic: "mychannel:main",
event: "msg",
payload: %{body: "hello there"}
}
have failed.
I am definitely missing something.
Best regards,
Yuri
--
You received this message because you are subscribed to the Google Groups "phoenix-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phoenix-talk...@googlegroups.com.
To post to this group, send email to phoeni...@googlegroups.com.
Visit this group at http://groups.google.com/group/phoenix-talk.
To view this discussion on the web visit https://groups.google.com/d/msgid/phoenix-talk/d81fb01a-85b6-4ff4-ada9-fc84f317a300%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "phoenix-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/phoenix-talk/H9z_5fb7vr4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to phoenix-talk...@googlegroups.com.
To post to this group, send email to phoeni...@googlegroups.com.
Visit this group at http://groups.google.com/group/phoenix-talk.
To view this discussion on the web visit https://groups.google.com/d/msgid/phoenix-talk/0D508957-F993-4082-B40C-E9D2BA0B6182%40chrismccord.com.
def join("user:" <> token, _params, socket) do
case Phoenix.Token.verify(socket, "user socket", token, max_age: @max_age) do
{:ok, ^socket.assigns.user_id} -> {:ok, socket}
{:error, _reason} -> {:error, "permission denied"}
end
end