Search⌘ K

Strategy 2: Communicating via I/O with Ports

Explore how Elixir uses ports to securely communicate with external software, ensuring fault tolerance and stability. Understand the benefits of asynchronous communication, the role of ports in embedded systems with Nerves, and real-world applications like Le Tote's automation. This lesson guides you through practical usage of ports for integrating third-party programs without risking system shutdowns.

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:

C++
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 ...