【问题标题】:Phoenix: Broadcasting from IEx consolePhoenix:从 IEx 控制台广播
【发布时间】:2015-05-28 04:35:39
【问题描述】:

我已经构建了一个小型聊天应用程序,如下所示: https://github.com/chrismccord/phoenix_chat_example/blob/master/web/channels/room_channel.ex

并且无法弄清楚如何在一个主题中向所有用户广播一条消息。在上面的应用程序中(没有像我使用的那样更新到 v0.13),我该怎么做?以下是我没有运气的尝试:

Phoenix.PubSub.broadcast Chat.PubSub, "new:msg", "hello from the console"
Phoenix.PubSub.broadcast Chat.Endpoint, "new:msg", "hello from the console"
Phoenix.PubSub.broadcast Chat.RoomChannel, "new:msg", "hello from the console"

它们都不起作用...其中一些抛出异常:(

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

    由于您使用的是 0.13,因此您需要从端点广播,并且您需要提供主题、事件和有效负载(作为地图)。试试这个:

    Chat.Endpoint.broadcast("rooms:lobby", "new:msg", %{message: "hello from the console"})
    

    这个 sn-p 对你的频道和客户端代码做了一些假设,所以如果它不起作用,请提供你的路由器、频道和 js 代码,以便我进一步提供帮助。

    【讨论】:

      猜你喜欢
      • 2016-03-01
      • 2016-11-20
      • 2019-01-11
      • 1970-01-01
      • 2018-11-11
      • 2018-12-24
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多