Last active
July 29, 2017 14:11
-
-
Save shahryarjb/409acb988a704fe7c69581c27ee403e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Chat do | |
use Application | |
def start(_type, _args) do | |
Chat.Supervisor.start_link | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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