Comply with the erlang IO protocol
Below is a basic IO server/device implementation, it receives an :io_request
message and responds with a :io_reply
message.
defmodule MYIODevice do
def listen() do
receive do
{:io_request, from, reply_as, {:put_chars, :unicode, message}} ->
send(from, {:io_reply, reply_as, :ok})
IO.puts(message)
IO.puts("I see you")
listen()
end
end
end
pid = spawn_link(MYIODevice, :listen, [])
IO.puts(pid, "Hey there")
The above code outputs the following to stdout:
Hey there
I see you
The first argument of IO.puts/2
is a pid representing the device that is being written to, it is generally defaulted to :stdio
)
The documentation is dense, enjoy! Erlang IO Protocol
H/T Brian Dunn
Tweet