我试图在我的应用程序中广播到不同的 channel ,但我无法让它工作。我也想写一个测试,但我不确定如何。
据我所知,我成功地从 notification_channel 广播了消息,但没有在 chat_channel 中收到。
通知应发送到聊天。
notification_channel.ex
def handle_in("new:group:recommendation", msg, socket) do
payload = %{
message: msg["message"],
url: msg["url"],
title: msg["title"],
user_name: get_name_of_user(socket.assigns.user_grapqhl_id),
user_grapqhl_id: socket.assigns.user_grapqhl_id
}
IO.puts "incomming"
IO.inspect msg
Enum.map(msg["groups"], fn(x) ->
App.Endpoint.broadcast_from! self(), "chat:"<>x,
"new:recommendation", payload
end)
{:reply, :ok, socket}
end
chat_channel.ex
def handle_in("new:recommendation", msg, socket) do
IO.puts "i am a recommendation !"
IO.inspect msg
chat_msg = %{
"creator_id" => msg["user_grapqhl_id"],
"text" => msg["message"],
"creator_name" => msg["user_name"]
}
broadcast! socket, "new:msg", create_chat_msg(chat_msg,socket)
{:reply, :ok, socket}
end
测试
test "do we send a new:recommendation to chat ?", %{guardian_token: guardian_token} do
nils_base_64 = Base.encode64("user:nils")
{:ok, socket} = connect(UserSocket, %{})
{:ok, _, socket1} = subscribe_and_join(socket, "notifications:"<>nils_base_64, %{"guardian_token" => guardian_token})
{:ok, _, socket} = subscribe_and_join(socket1, "chat:Y2hhdDpjaGF0Mw==", %{"guardian_token" => guardian_token})
payload = %{
"message" => "look at this cool thing!",
"url" => "link to stuff",
"title" => "AWESOME EVENT",
"groups" => ["Y2hhdDpjaGF0Mw==", "Y2hhdDpwdWJsaWM="]
}
reply = %{message: "look at this cool thing!", title: "AWESOME EVENT", url: "link to stuff", user_grapqhl_id: nils_base_64, user_name: "Nils Eriksson"}
ref = push socket1, "new:group:recommendation", payload
assert_reply ref, :ok
assert_broadcast "new:recommendation", ^reply
end
此测试通过,我可以通过更改
reply
使其失败或评论广播。我无法通过更改
handle_in
使其失败接收 fail:please
在 chat_channel
.如果我发送更改此内容,它会提示这一点
ref = push socket1, "new:group:recommendation", payload
至 ref = push socket, "new:group:recommendation", payload
在这种情况下并不奇怪。这是电线上的东西。
Process mailbox:
%Phoenix.Socket.Message{event: "init:msgs", payload: %{messages: []}, ref: nil, topic: "chat:Y2hhdDpjaGF0Mw=="}
%Phoenix.Socket.Broadcast{event: "new:recommendation", payload: %{message: "look at this cool thing!", title: "AWESOME EVENTs", url: "link to stuff", user_grapqhl_id: "dXNlcjpuaWxz", user_name: "Nils Eriksson"}, topic: "chat:Y2hhdDpjaGF0Mw=="}
%Phoenix.Socket.Message{event: "new:recommendation", payload: %{message: "look at this cool thing!", title: "AWESOME EVENTs", url: "link to stuff", user_grapqhl_id: "dXNlcjpuaWxz", user_name: "Nils Eriksson"}, ref: nil, topic: "chat:Y2hhdDpjaGF0Mw=="}
我使用 channel 身份验证,因为我使用的 elm 包不支持套接字级别的身份验证。所以这就是
chat
中的样子 def join("chat:" <> chat_id, %{"guardian_token" => token}, socket) do
IO.puts chat_id
case sign_in(socket, token) do
{:ok, authed_socket, _guardian_params} ->
Process.flag(:trap_exit, true)
send(self, {:after_join})
[_type, node_chat_id] = Node.from_global_id(chat_id)
{:ok, assign(authed_socket, :chat_id, node_chat_id)}
{:error, reason} ->
IO.puts "Can't join channel cuz: " <> reason
# handle error TODO
end
结尾
最佳答案
由于您使用 broadcast_from/4
来自您的 Endpoint
.你应该使用 handle_info/2
在您的 chat_channel
:
alias Phoenix.Socket.Broadcast
...
def handle_info(%Broadcast{topic: _, event: ev, payload: payload}, socket) do
IO.puts ev
IO.inspect payload
# do something with ev and payload( push or broadcast)
{:noreply, socket}
end
或者您可以从客户端收听该事件:
chatChannel.on("new:recommendation", resp => {
// doSomething with response
}
编辑:
让我们解释一下
channel
和 PubSub
系统工作。当你想广播或推送一个带有有效载荷的事件时。首先它会发送到
PubSub
系统,然后是 PubSub
系统会将其发送给所有订阅者进程( channel
),主题为 channel
在 PubSub
注册自己系统。当您使用
Endpoint.broadcast_from/4
时从您的服务器广播事件。PubSub
系统将接收一个带有有效负载的事件并将该事件广播到该 channel 注册的主题。channel 会触发
handle_out
回调并将消息推送到客户端。所以在您的
chat_channel
你不需要handle_in
"new:recommendation"事件。您的客户只需要收听该事件。chatChannel.on("new:recommendation", resp => {
// do something with response
}
让我重写你的测试:
setup do
nils_base_64 = Base.encode64("user:nils")
{:ok, socket} = connect(UserSocket, %{})
{:ok, _, socket} = subscribe_and_join(socket, "notifications:"<>nils_base_64, %{"guardian_token" => guardian_token})
{:ok, socket: socket}
end
test "do we send a new:recommendation to chat ?", %{socket: socket} do
MyApp.Endpoint.subscribe("chat:Y2hhdDpjaGF0Mw==")
payload = %{
"message" => "look at this cool thing!",
"url" => "link to stuff",
"title" => "AWESOME EVENT",
"groups" => ["Y2hhdDpjaGF0Mw==", "Y2hhdDpwdWJsaWM="]
}
reply = %Phoenix.Socket.Broadcast{message: "look at this cool thing!",
title: "AWESOME EVENT",
url: "link to stuff",
user_grapqhl_id: nils_base_64,
user_name: "Nils Eriksson"}
ref = push socket, "new:group:recommendation", payload
assert_reply ref, :ok
assert_receive ^reply
end
来自
subscribe
对于您想收听的主题,您可以确保您的 channel 收到了带有 assert_receive
的消息。 .那就是测试
broadcast
的方法到不同的 channel 。试一试告诉我,测试会通过的。
关于sockets - 广播到不同 channel Phoenix 1.1.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551516/