Hi. I am learning Elixir recently, decided to make a small simple project “Chat Room” for myself to learn some OTP. Please help to implement “message history” (to store 100 messages for example) and list of chat room participants (using join/leave functions).
I do not understand clearly how I can send a message that is stored in the history of the last 100 messages and broadcast to chat members (I know I need to use pid but all other stuff is unclear)
I made some type of pseudocode for ‘Message’ & ‘Room’ modules (and I need ‘User’ module too as I understand) but I have a big troubles with structs - how and where to define them correctly and how integrate them into my existing code
Message:
%Message{from: "username", message: "hello world"}
Room:
%Room{
users: [%User{name: "user1", pid: PID }],
history: [%Message{from: "username", message: "hello world"} ... ],
name: "room name"
}
And some pseudocode callback
# def handle_call(:chat_history, {FromPid, _Ref}, %Room{history: history, users: users} = room) do
# users
# |> Enum.map(&(&1.pid))
# |> then(fn pids -> FromPid in pids end)
# |> case do
# true -> {:reply, history, room}
# false -> {:reply, {:error, "not a chat member"}, room}
# end
Thx for help, this is my code:
server.ex
Summary
defmodule Chat.Server do
use GenServer
#Client
def start_link() do
GenServer.start_link(__MODULE__, [])
end
def send_message(pid, message) do # send_message
GenServer.cast(pid, message)
end
def get_messages(pid) do # get list of messages ref to pid
GenServer.call(pid, :get_messages) # :get_messages - 1st arg of handle_call func
end
def remove_message(pid, message) do
GenServer.cast(pid, {:remove, message}) # :remove will pattern match with callback
end
def stop(pid) do
GenServer.stop(pid, :normal, :infinity) # pid, shutdown reason(:normal - default), timeout(:infinity - default)
end
def init(list) do
{:ok, list}
end
def terminate(_reason, list) do
IO.puts("All messages are done")
IO.inspect(list)
:ok
end
def handle_cast({:remove, message}, list) do # pm ':remove' with tuple in remove_message
updated_list = Enum.reject(list, fn(i) -> i == message end)
{:noreply, updated_list}
end
def handle_cast(message, list) do
updated_list = [message|list]
{:noreply, updated_list}
end
def handle_call(:get_messages, _from, list) do # testing (atom, 2 elem tuple - colors PID, existing state of message list)
{:reply, list, list}
end
end