diff --git a/lib/chat_web/live/chat_live.ex b/lib/chat_web/live/chat_live.ex new file mode 100644 index 0000000..c827149 --- /dev/null +++ b/lib/chat_web/live/chat_live.ex @@ -0,0 +1,34 @@ +defmodule ChatWeb.RoomLive do + use ChatWeb, :live_view + alias ChatWeb.MessageComponent + + def mount(%{"id" => room_id}, _session, socket) do + # Join the room, subscribe to PubSub topic + IO.inspect("Joining room #{room_id}") + {:ok, assign(socket, room_id: room_id, messages: [%{id: 1, content: "Hello"}])} + end + + def handle_event("send_message", %{"message" => message}, socket) do + # Broadcast the message to all users in the room + # Update the messages list + IO.inspect("Received #{message}") + {:noreply, socket} + end + + def render(assigns) do + ~H""" +
+

Chat Room <%= @room_id %>

+
+ <%= for message <- @messages do %> + <.live_component module={MessageComponent} id={message.id} message={message} /> + <% end %> +
+
+ + +
+
+ """ + end +end diff --git a/lib/chat_web/live/message_component.ex b/lib/chat_web/live/message_component.ex new file mode 100644 index 0000000..22a3b91 --- /dev/null +++ b/lib/chat_web/live/message_component.ex @@ -0,0 +1,11 @@ +defmodule ChatWeb.MessageComponent do + use ChatWeb, :live_component + + def render(assigns) do + ~H""" +
+ <%= @message.content %> +
+ """ + end +end diff --git a/lib/chat_web/router.ex b/lib/chat_web/router.ex index 64c81ca..9b2ad6c 100644 --- a/lib/chat_web/router.ex +++ b/lib/chat_web/router.ex @@ -18,6 +18,8 @@ defmodule ChatWeb.Router do pipe_through :browser get "/", PageController, :home + + live "/chat/:id", RoomLive end # Other scopes may use custom stacks.