Create a gist now

Instantly share code, notes, and snippets.

@shahryarjb /chat.ex
Last active Jul 29, 2017

What would you like to do?
defmodule Chat do
use Application
def start(_type, _args) do
Chat.Supervisor.start_link
end
end
defmodule Chat.Server do
use GenServer
#client side
def start_link do
GenServer.start_link(__MODULE__, [], name: :chat_room)
end
def get_masgs do
GenServer.call(:chat_room, :get_masgs)
end
def add_msg(msg) do
GenServer.cast(:chat_room, {:add_msg, msg})
end
#server side/ callback finction
def init(msgs) do
{:ok, msgs}
end
def handle_call(:get_masgs, _from, msgs) do
{:reply, msgs, msgs}
end
def handle_cast({:add_msg, msg}, msgs) do
{:noreply, [msg | msgs]}
end
end
defmodule Chat.Supervisor do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, [])
end
def init(_) do
children = [
worker(Chat.Server, [])
]
supervise(children, strategy: :one_for_one)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment