Lesson 9: Basic Process Communication
Send and receive messages between processes, keep state in a receive loop, and build a small chat room process
Lesson 08 used ! and receive just enough to get a result out of a spawned process. Processes share no memory, so messages are the only way they cooperate. Here we look at how messages are queued, matched, and answered, then build a chat room process.
Mailboxes
Every process has a mailbox, a queue of messages in arrival order. Pid ! Message copies the message into the mailbox of Pid and returns at once. The sender does not wait for the receiver to read it. The shell is a process, so it can mail itself, and flush() prints and removes everything in the shellβs mailbox.
1> self() ! hello.hello2> self() ! {chat, "alice", "hi"}.{chat,"alice","hi"}3> flush().Shell got helloShell got {chat,"alice","hi"}okThe value of a send expression is the message itself. Sending to a pid never fails, even when the process is gone.
4> Dead = spawn(fun() -> ok end).<0.89.0>5> is_process_alive(Dead).false6> Dead ! are_you_there.are_you_thereThe message is dropped silently. This is deliberate. A process can die between a check and the send, so a check would prove nothing. If you need to know that a message was handled, ask for a reply.
Receiving with Patterns
receive has clauses like case. It takes a message out of the mailbox, binds the variables of the matching clause, and runs its body. If the mailbox is empty, the process sleeps until a message arrives.
7> Pid = spawn(fun() ->7> receive7> {chat, From, Text} -> io:format("~s says ~s~n", [From, Text]);7> Other -> io:format("unexpected: ~p~n", [Other])7> end7> end).<0.93.0>8> Pid ! {chat, "alice", "hi"}.alice says hi{chat,"alice","hi"}9> is_process_alive(Pid).falseOne receive handles one message. The fun then returned, so the process ended.
Selective Receive
receive does not always take the oldest message. It checks the oldest message against every clause. If none match, that message stays in the mailbox and the next one is tried. This is called selective receive.
10> self() ! first, self() ! second, self() ! third.third11> receive third -> got_third end.got_third12> receive Msg -> Msg end.first13> flush().Shell got secondokThe skipped messages kept their order. Erlang guarantees that messages from one process to another arrive in the order they were sent. There is no guarantee about how messages from different senders interleave.
Selective receive lets a process wait for one specific reply while unrelated messages queue up. The cost is that every receive scans from the front of the mailbox. If some messages never match, the mailbox grows and scans get slower, so long-running loops usually end with a catch-all clause that drops unknown messages.
Timeouts
A receive for a message that never comes blocks forever. The after clause sets a limit in milliseconds.
14> receive never_sent -> ok after 2000 -> timeout end.timeoutafter 0 checks the mailbox without waiting.
15> self() ! leftover.leftover16> receive Any -> {got, Any} after 0 -> empty end.{got,leftover}17> receive Any2 -> {got, Any2} after 0 -> empty end.emptyLoops That Hold State
A process that should handle many messages calls itself after each one. Its state is the argument of the loop function. Variables never change. The next iteration gets a new value.
Create counter.erl:
-module(counter).-export([start/0, loop/1]).
start() -> spawn(?MODULE, loop, [0]).
loop(Count) -> receive increment -> loop(Count + 1); {get, From} -> From ! {count, Count}, loop(Count); stop -> ok end.Every call to loop/1 is the last thing its clause does. That makes it a tail call, as in lesson 03, so the loop runs forever in constant stack space. The stop clause does not call loop/1, so the function returns and the process ends.
18> c(counter).{ok,counter}19> C = counter:start().<0.110.0>20> C ! increment, C ! increment, C ! increment.increment21> C ! {get, self()}.{get,<0.84.0>}22> receive {count, N} -> N end.3The count lives in exactly one process, and that process handles one message at a time. Two increments can never interleave, so there is nothing to lock.
Request and Reply
{get, self()} shows the basic request pattern. The request carries the pid to reply to. The weak spot is the reply. A pattern like {count, N} matches any such message, including a late reply to an earlier request that timed out.
make_ref/0 returns a reference, a value that is unique in the running system. Put a fresh one in the request, have the server copy it into the reply, and match on it.
23> Server = spawn(fun() ->23> receive {From, Ref, {add, A, B}} -> From ! {Ref, A + B} end23> end).<0.118.0>24> Ref = make_ref().#Ref<0.2733087087.2544107521.156637>25> Server ! {self(), Ref, {add, 2, 3}}.{<0.84.0>,#Ref<0.2733087087.2544107521.156637>,{add,2,3}}26> self() ! {make_ref(), 999}.{#Ref<0.2733087087.2544107521.156656>,999}27> receive {Ref, Result} -> Result after 1000 -> timeout end.5Ref is already bound, so the pattern {Ref, Result} only matches a tuple carrying that exact reference. The stray message from line 26 stays in the mailbox.
Registered Names
Passing pids around gets awkward for long-lived services. register/2 gives a process a name, an atom that works in place of the pid when sending. whereis/1 looks the pid up.
28> register(hits, counter:start()).true29> hits ! increment.increment30> whereis(hits).<0.125.0>31> hits ! stop.stop32> whereis(hits).undefinedThe name disappears when the process ends. Unlike sending to a dead pid, sending to a name nobody holds raises badarg. Atoms are never garbage collected, so name a few well-known processes, not one per user.
A Chat Room Process
A chat room is a loop whose state is the member list, kept as {Pid, Name} tuples. Callers should not need to know the message formats, so the module wraps each one in a function.
Create chat_room.erl:
-module(chat_room).-export([start/0, join/2, leave/1, broadcast/2, members/1]).-export([loop/1]).
start() -> spawn(?MODULE, loop, [[]]).
join(Room, Name) -> Room ! {join, self(), Name}, ok.
leave(Room) -> Room ! {leave, self()}, ok.
broadcast(Room, Text) -> Room ! {broadcast, self(), Text}, ok.
members(Room) -> call(Room, members).
call(Room, Request) -> Ref = make_ref(), Room ! {call, self(), Ref, Request}, receive {Ref, Reply} -> Reply after 5000 -> {error, timeout} end.
loop(Members) -> receive {join, Pid, Name} -> send_all(Members, {chat, "room", Name ++ " joined"}), loop([{Pid, Name} | Members]); {leave, Pid} -> loop(lists:keydelete(Pid, 1, Members)); {broadcast, Pid, Text} -> case lists:keyfind(Pid, 1, Members) of {Pid, Name} -> send_all(Members, {chat, Name, Text}); false -> ignored end, loop(Members); {call, From, Ref, members} -> From ! {Ref, lists:sort([Name || {_Pid, Name} <- Members])}, loop(Members) end.
send_all(Members, Message) -> [Pid ! Message || {Pid, _Name} <- Members], ok.The API functions run in the callerβs process, so self() there is the callerβs pid. loop/1 runs in the room process. join, leave, and broadcast send and return without waiting. members/1 needs an answer, so it goes through call/2, which sends a reference and waits up to five seconds for the matching reply.
1> c(chat_room).{ok,chat_room}2> Room = chat_room:start().<0.91.0>3> chat_room:join(Room, "alice").ok4> Bob = spawn(fun() ->4> chat_room:join(Room, "bob"),4> receive {chat, "alice", Text} -> io:format("bob got: ~s~n", [Text]) end4> end).<0.94.0>5> chat_room:members(Room).["alice","bob"]6> chat_room:broadcast(Room, "hello room").bob got: hello roomok7> flush().Shell got {chat,"room","bob joined"}Shell got {chat,"alice","hello room"}ok8> is_process_alive(Bob).false9> chat_room:members(Room).["alice","bob"]Bobβs process ended after one message, but the room still lists him and keeps sending to a dead pid. Sends never fail, so the room cannot notice. Lesson 10 fixes this.
Exercises
Exercise 1: Ping Pong
Write a pong process that answers every {ping, From} with pong and stops on stop. Then write ping/2, which sends N pings, waits for each reply, and returns how many it got.
% pingpong.erl exports start/0, ping/2 and loop/0% Expected:% P = pingpong:start().% pingpong:ping(P, 3) returns 3Exercise 2: Synchronous Counter
Add API functions to counter.erl so callers never write a message by hand. value/1 must use a reference and return {error, timeout} after one second.
increment(Counter) -> ok.value(Counter) -> 0.
% Expected:% C = counter:start(), counter:increment(C), counter:value(C) returns 1% counter:value(spawn(fun() -> ok end)) returns {error,timeout}Exercise 3: Message Count
Extend chat_room so the room counts broadcasts. Add message_count/1 as a second synchronous call. The loop state becomes two arguments or a tuple.
% Expected: after two broadcasts from a member% chat_room:message_count(Room) returns 2Key Takeaways
!is asynchronous. It copies the message into the receiverβs mailbox and returns at once, even if the pid is deadreceivepattern matches against the mailbox and leaves messages that match no clause where they are- Message order is kept between one sender and one receiver. Nothing is promised across different senders
afterputs a time limit on waiting.after 0polls the mailbox- State is the argument of a tail-recursive loop. One process owns it, so no locks are needed
- A fresh reference from
make_ref/0ties a reply to its request register/2names a long-lived process. Sending to an unregistered name is an error- API functions hide the message protocol so callers only see ordinary function calls
Interactive Koans
Koans - Test Your Understanding
Fill in the blanks and press Enter or click Run to test your knowledge!
What is the value of a send expression?
π‘ Hint
π Explanation
Two messages are waiting. Which one does the second receive return?
π‘ Hint
π Explanation
The loop keeps a count as its argument. What does the shell receive?
π‘ Hint
π Explanation
Which variable in the receive pattern makes sure only the reply to this request matches?
π‘ Hint
π Explanation
Which function gives the process a name so the send on the next line works?
π‘ Hint
π Explanation
The room process is already gone. What does the caller get after the timeout?
π‘ Hint
π Explanation
Finished this lesson?
Mark it as complete to track your progress
This open source tutorial is brought to you by East Branch Software - we build reliable software systems.
Found an issue? Edit this page on GitHub or open an issue