Google News
logo
Elixir - Interview Questions
Describe the process of sending and receiving messages in Elixir
To send messages in Elixir, a developer needs to know the ID of a process. The overall syntax looks as follows.
iex > send(id(), :text)

:text​

The text after the “:” is what the system returns.

A developer should mention that after the system sent a message from one process to another, it will not get retrieve until the “receive” function is called. For the time being, it will stay stored in the mailbox.

To see which messages there are in the inbox of each process, call Process.info/
iex > Process.info(self(), :messages)

{:messages, [:hi]}​
To get a message from one process to another, developers run the receive/1 macro.
iex > receive do :text -> IO.puts "Hello." end

Hello.

:ok

iex > Process.info(self(), :messages)

{:messages, []}​

It’s important to make sure that the function matches the message atom.
Advertisement