Skip to content

Commit

Permalink
Send existing messages to the client when someone joins the chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Marta Medio committed Mar 11, 2019
1 parent f3d64a0 commit 76de1c6
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/chat_web/channels/my_chat_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule ChatWeb.MyChatChannel do
use ChatWeb, :channel

def join("my_chat:lobby", payload, socket) do
send(self(), :after_join)
if authorized?(payload) do
{:ok, socket}
else
Expand All @@ -23,6 +24,15 @@ defmodule ChatWeb.MyChatChannel do
{:noreply, socket}
end

def handle_info(:after_join, socket) do
Chat.Message.get_messages()
|> Enum.each(fn msg -> push(socket, "shout", %{
name: msg.name,
message: msg.message,
}) end)
{:noreply, socket}
end

# Add authorization logic here as required.
defp authorized?(_payload) do
true
Expand Down

0 comments on commit 76de1c6

Please sign in to comment.