Skip to content

Instantly share code, notes, and snippets.

@sjahandideh
Created February 9, 2016 01:20
Show Gist options
  • Save sjahandideh/fa619f98966ae077f1fa to your computer and use it in GitHub Desktop.
Save sjahandideh/fa619f98966ae077f1fa to your computer and use it in GitHub Desktop.
sample elixir server client
defmodule Server do
def start(callback_module) do
spawn fn ->
callback_module |> run
end
end
def run(callback_module) do
receive do
message ->
message |> callback_module.handle_message
callback_module |> run
end
end
end
defmodule Client do
def handle_message("ping"), do: IO.puts("pong")
def handle_message("pong"), do: IO.puts("ping")
def handle_message(_other), do: IO.puts("Not playing this game! ¯\_(ツ)_/¯")
end
# server = Server.start Client
# send server, "ping" -> returns "pong"
# send server, "pong" -> returns "ping"
# send server, "blah" -> returns "Not playing this game! ¯\_(ツ)_/¯"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment