Skip to content

Instantly share code, notes, and snippets.

@bmitch
Created June 28, 2017 23:21
Show Gist options
  • Save bmitch/4bcfb83abf830e7afd6ca51c728a7968 to your computer and use it in GitHub Desktop.
Save bmitch/4bcfb83abf830e7afd6ca51c728a7968 to your computer and use it in GitHub Desktop.
ticker.ex
defmodule Ticker do
@interval 2000
@name :ticket
def start do
pid = spawn(__MODULE__, :generator, [[]])
:global.register_name(@name, pid)
end
def register(client_id) do
send :global.whereis_name(@name), { :register, client_id }
end
def generator(clients) do
receive do
{ :register, pid } ->
IO.puts "registering #{inspect pid}"
generator([pid|clients])
after
@interval ->
IO.puts "tick"
Enum.each clients, fn client ->
send client, { :tick }
end
generator(clients)
end
end
end
defmodule Client do
def start do
pid = spawn(__MODULE__, :receiver, [])
Ticker.register(pid)
end
def receiver do
receive do
{ :tick } ->
IO.puts "tock in client"
IO.puts :os.system_time(:millisecond)
receiver
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment