One of the great things about Erlang is that it's got distributed systems built into it at a fundamental level. This is one of the reasons that people say the best feature of Elixir is Erlang. I'm going to show you how to experiment with this.

When you start an iex session, you can give it a name. Open up two terminals side by side. In one of them, type iex --name "foo@0.0.0.0". In the other, type iex --name "bar@0.0.0.0". You've seen sending messages to processes before. The shell in iex is just a process as well, so you can send it messages. Since we don't know each shell's pid just yet, let's register one of them as a local named process. Do this in the foo shell, with: ```elixir :erlang.register(:shell, self) ``` This registers this pid locally so that it can be referenced with the atom:shell. Now, we're going to send the pid from the bar shell into this shell, so let's set up a receive block to capture whatever we're sent as the local variablebar_pid`. In "foo", run the following: elixir bar_pid = receive do pid -> pid end Now it's blocking waiting for a message. Let's go over to "bar" and send its pid to "foo": elixir {:shell, :"foo@0.0.0.0"} <- self You should see the "foo" shell stop blocking. Now you can send a message to "bar" using the pid you were sent. Try it: elixir bar_pid <- "hey there!" You can verify you received the message in "bar" by flushing all the messages from the mailbox: elixir flush() ## Summary That's it. This was just a quick look at using the erlang distribution mechanism from Elixir. You saw that sending a message to a process on another node is identical to sending a message to a local process. See you soon!

Josh Adams

I've been building web-based software for businesses for over 18 years. In the last four years I realized that functional programming was in fact amazing, and have been pretty eager since then to help people build software better.

  1. Comments for Distribution

You must login to comment

You May Also Like