Elixir foundationSort Order

Elixir: Listening for Messages from Another Process - [022]

Previously we wrote the Botiful.Render.Status module which we will use to check the status of network calls. In this post we will show you how to use handle_info to listen for messages from the status checker.

We'll begin by modifying our Botiful.Render.Engine

Starting the Status GenServer

We can start the Status checker inside the Engine module with the following.

Note: I forgot to show the coding part of starting the Status GenServer but this code below should cover it. Use this to patch your code as needed.

defmodule Botiful.Render.Engine do  # ...  alias Botiful.Render.Status    def init(url) do    # ...    {:ok, requests} = Agent.start_link(fn -> [] end)    {:ok, responses} = Agent.start_link(fn -> [] end)       {:ok, status} = Status.start_link({      %{requests: requests, responses: responses},      self()    })    {:ok, %{      conn: conn,       requests: requests,       responses: responses,       status: status,       origin: "https://www.artellectual.com"    }}  end  # ...end

Now that we've started the Status checker we need to implement the callbacks to handle the message from the Status module.

defmodule Botiful.Render.Engine do  # ...  def handle_info(:completed_and_valid, state) do    requests = Agent.get(state.requests, fn state -> state end)    responses = Agent.get(state.responses, fn state -> state end)        IO.inspect(requests)    IO.inspect(responses)    {:noreply, state}  end  def handle_info(:completed, state) do    {:noreply, state}  end  # ...end

We can now run the Render engine like so.


You should see the output of the list of urls and responses.