广播到凤凰城1.1.6的不同频道

时间:2016-07-24 11:26:09

标签: sockets elixir phoenix-framework phoenix-channels

我试图在我的应用中广播到不同的频道,但我无法让它发挥作用。我也试着写一个测试,但我不知道怎么做。

从我收集到的内容中,我成功地从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=="}

我使用频道验证,因为我使用的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

2 个答案:

答案 0 :(得分:5)

由于您使用了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
}

修改

让我们解释channelPubSub系统如何运作。

当你想要播放或推送带有效载荷的事件时。首先它会发送到PubSub系统,然后PubSub系统会将它发送到所有订阅者进程(channelchannel使用PubSub系统注册的主题。

当您使用Endpoint.broadcast_from/4从您的服务器广播事件时。PubSub系统将收到一个带有效负载的事件,并将该事件广播到该频道已注册的主题。

频道将触发handle_out回调并将消息推送到客户端。 因此,在您的chat_channel中,您不需要handle_in&#34; new:recommended&#34; event.Your客户只需要听一下这个事件。

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到您想要收听的主题,您可以确保您的频道收到assert_receive的邮件。 这是将broadcast测试到其他渠道的方法。

尝试告诉我。测试将通过。

答案 1 :(得分:1)

App.Endpoint.broadcast topic, event, payload与聊天频道的主题结合使用。它应该工作。