Book Image

Elixir Cookbook

By : Paulo Pereira
Book Image

Elixir Cookbook

By: Paulo Pereira

Overview of this book

Table of Contents (16 chapters)
Elixir Cookbook
About the Author
About the Reviewers

Sending messages between processes

In Elixir, communication between processes is performed via message passing. Each process has a mailbox where messages from the "outside" world are placed, waiting to be processed. Once that happens, if a response is required, another message will be sent, and another mailbox will get a message!

Getting ready

To get started, go to the code repository where the messages.ex file is located and open a new IEx terminal session. The IEx terminal session will also be an actor in this recipe! We will send messages from it to the process containing the code defined in the module.

How to do it…

Follow these steps to send messages between processes:

  1. Once our session is started, load and compile the messages.ex module:

    iex(1)> c "messages.ex"
  2. Next, spawn a new process containing the code from our module:

    iex(2)> {:ok, pid} = Messages.start_link
    {:ok, #PID<0.61.0>}
  3. To make things easier, we will register our newly spawned process with a name: