Reputation: 1274
I'm trying to figure out on how to trigger the channel broadcast from the controller.
Example
def create(conn, %{"message" => message_params}) do
with {:ok, %Message{} = message} <- Chat.create_message(message_params) do
conn
# TRIGGER CHANNEL BROADCAST "SHOUT" HERE
|> put_status(:created)
|> put_resp_header("location", message_path(conn, :show, message))
|> render("show.json", message: message)
end end
water_cooler channel
defmodule NotificationWeb.WaterCoolerChannel do
use NotificationWeb, :channel
def join("water_cooler:lobby", _payload, socket) do
{:ok, socket}
end
def handle_in("shout", payload, socket) do
broadcast socket, "shout", payload
{:noreply, socket}
end
end
I've tried to use NotificationWeb.broadcast(topic, event, msg)
but confused on what to put in
topic = "water_cooler" ?
event = ?
message = ?
Upvotes: 0
Views: 694
Reputation: 15045
It's possible to broadcast a message using broadcast function of the NotificationWeb.Endpoint
.
topic
here is the room, in which you want to broadcast (for example water_cooler:lobby
)
event
is the name of the event, you're expecting to receive on the client
msg
is the additional information within the event
Considering you have something like this on the front-end part:
channel.on("new_msg", payload => {
// process new message
})
then event
equals new_msg
, and msg
is the data, which payload
variable within this event will contain
Upvotes: 2