Skip to content

Instantly share code, notes, and snippets.

@lilrooness
Created June 25, 2019 10:31
Show Gist options
  • Select an option

  • Save lilrooness/6d56ea21429a075133d15a7d5f995781 to your computer and use it in GitHub Desktop.

Select an option

Save lilrooness/6d56ea21429a075133d15a7d5f995781 to your computer and use it in GitHub Desktop.
# Spawn example
spawn(fn() -> IO.inspect(5 * 5) end)
# Spawn extended example
a = fn(x) ->
spawn(fn() ->
:timer.sleep(1000) # Send this process to sleep for 1 second
IO.inspect("I'm done!") # Print "I'm done to the console"
end)
x * x # return x squared
end
a.(5)
# Spawn in a list
square_me = fn(x) -> IO.inspect(x * x) end
for n <- [1,2,3,4,5], do: spawn(fn() -> square_me.(n) end)
# Messages
# Sending Messages
p_id = spawn(fn() ...... end)
send(p_id, {:data, "some message ..."})
# Receiving Messages
receive do
{:data, data} ->
IO.inspect(data, label: "I received a message!")
end
# All Together
receiver = spawn(fn() ->
receive do
{:data, data} ->
IO.inspect(data, label: "I received a message!")
end
end)
send(receiver, {:data, "something!!!"})
# square me with messages
a = fn(x) ->
requester = self()
pid = spawn(fn() ->
answer = x * x
send(requester, {:answer, answer})
end)
end
a.(5)
y = receive do
{:answer, answer} ->
IO.inspect("It came back!!!")
answer
end
IO.inspect(y)
# Simple Chat Server
defmodule ChatServer do
def start(clients) do
Process.register(self(), ChatServer)
server_loop(clients)
end
defp server_loop(clients) do
receive do
{:message, {_sender, _message}} = new_message ->
for client <- clients, do: send(client, new_message)
end
server_loop(clients)
end
end
defmodule ChatClient do
def start(client_connection) do
client_loop(client_connection)
end
defp client_loop(client_connection) do
receive do
{:message_from_client_connection, message} ->
send(ChatServer, {self(), message})
{:message_from_server, message} ->
send(client_connection, message)
end
client_loop(client_connection)
end
end
# Chat Server Demo
me = self()
spawn(fn() -> ChatServer.start([me]) end)
send(ChatServer, {:message, {"Joe", "I really hope this works"}})
m = receive do
{:message, {sender, message}} ->
IO.inspect(message, label: "I received - #{message} - from #{sender}")
message
end
# Distribution .... Here we go
iex --sname server
iex --sname c1
iex --sname c2
defmodule ChatServer do
def start(clients) do
spawn(fn ->
:global.register_name(ChatServer, self())
server_loop(clients)
end)
end
defp server_loop(clients) do
clients = receive do
{:message, {_sender, _message}} = new_message ->
IO.inspect(new_message, label: "new message")
for client <- clients, do: send(client, new_message)
clients
{:new_client, pid} ->
IO.inspect(pid, label: "new client!")
send(pid, :welcome)
[pid | clients]
end
server_loop(clients)
end
end
ChatServer.start([])
Node.connect(:server@JoeF)
# CLIENT 1
send(:global.whereis_name(ChatServer), {:new_client, self()})
receive do
wm -> IO.inspect(wm, label: "woop!!")
end
receive do
{:message, message} -> IO.inspect(message, label: "new message!")
end
# CLIENT 2
send(:global.whereis_name(ChatServer), {:new_client, self()})
send(:global.whereis_name(ChatServer), {:message, {"C2", "OMG HI!"}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment