floof-webchat/lib/chat_web/channels/room_channel.ex
2022-11-26 23:54:11 +01:00

35 lines
930 B
Elixir

defmodule ChatWeb.RoomChannel do
use ChatWeb, :channel
@impl true
def join("room:lobby", payload, socket) do
if authorized?(payload) do
{:ok, socket}
else
{:error, %{reason: "unauthorized"}}
end
end
# Channels can be used in a request/response fashion
# by sending replies to requests from the client
@impl true
def handle_in("ping", payload, socket) do
{:reply, {:ok, payload}, socket}
end
# It is also common to receive messages from the client and
# broadcast to everyone in the current topic (room:lobby).
@impl true
def handle_in("shout", payload, socket) do
Chat.Floof.send_message(payload["name"], payload["message"])
# floof broadcasts back into the channel with correct `source` attached
#broadcast(socket, "shout", payload)
{:noreply, socket}
end
# Add authorization logic here as required.
defp authorized?(_payload) do
true
end
end