defmodule T do
  # This will run on alice@alicemachine
  def receiver() do
    receive do
      m ->
        IO.puts("Message: #{inspect(m)}")
    end

    receiver()
  end

  # This will run on alice@alicemachine
  def monitor(node) do
    Node.monitor(node, true)

    receive do
      m ->
        IO.puts("Monitor: #{inspect(m)}")
    end

    monitor(node)
  end

  # This will run on bob@bobmachine
  def sender(to) do
    send(to, DateTime.utc_now())
    Process.sleep(1000)
    sender(to)
  end

  # This will run on alice@alicemachine
  def start() do
    Node.connect(:bob@bobmachine)
    _monitor = spawn(T, :monitor, [:bob@bobmachine])
    receiver = spawn(&T.receiver/0)

    Node.spawn(:bob@bobmachine, fn -> T.sender(receiver) end)
  end
end