Skip to content

Instantly share code, notes, and snippets.

@shahryarjb
Last active July 29, 2017 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shahryarjb/409acb988a704fe7c69581c27ee403e4 to your computer and use it in GitHub Desktop.
Save shahryarjb/409acb988a704fe7c69581c27ee403e4 to your computer and use it in GitHub Desktop.
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