...

/

Strategy 2: Communicating via I/O with Ports

Strategy 2: Communicating via I/O with Ports

Learn how to communicate with external software using ports.

We'll cover the following...

Ports provide a safer alternative to integrate with external software. Each port starts the third-party software as a separate process in the operating system. If that port terminates, the Elixir code gets a message, and we can act accordingly. A segmentation fault in the external port won’t bring the Elixir system down.

It is possible that we’ve already spawned ports in our Elixir applications, like this:

Press + to interact
System.cmd("elixir", ["-e", "IO.puts 21 * 2"])

Now, try running this in the terminal below.

Please click the “Click to Connect” at the center of the terminal to start it.

Terminal 1
Terminal
Loading...

This command finds the elixir executable in the operating system and invokes it, passing the command-line arguments -e, for code evaluation, and the contents IO.puts 21 * 2. Then, System.cmd returns the result written to the standard ...