Applications
Now that we have most of the basics of processes in Elixir, let's try out some examples and applications.
There will be a progression through these examples. We will start pretty small and grow in complexity.
Ping pong
Let's start with a very basic example where one process sends a :ping message to another process. The receiving process will send a :pong message in response.
We will start with a module that looks very similar to the module we created for storing state in a process, except that we have no need for state, this module will only listen for the :ping messages and return :pong:
defmodule PingPong do
def start_link do
spawn_link(fn -> loop() end)
end
defp loop do
receive do
{:ping, sender} ->
send sender, {:pong, self}
end
loop
end
endWe start with the start_link/0 function that spawns a new process context and kicks off our internal loop. From the loop, we block with the receive do expression. Once the process receives a :ping...