Deploying Elixir Modules @ Different Nodes

Happy Coding !!

Google Image Seacrh Nodes Communication

This article explains about Elixir Module Deployment across multiple nodes from the scratch. Here we go with deep understanding of things like Node Creation, Establishing Node Connection and Deploying Modules across multiple Nodes.

First we go with module creation which will be shared across the Nodes.

Module Creation

Here we create an Elixir Module with filename my_node.ex and with convention we name our module as MyNode.

Create a file using your favorite editor. Here I am using vim .

$ vim mynode.ex

After creating the file copy the following code snippet and paste it inside the file mynode.ex.

defmodule MyNode do
def hello do
IO.puts "hi !! #{Node.self()}"

The code snippet contains a function MyNode.hello/0 and this function will greet you with hi !! and node name form where you are calling this function. The Node.self() returns the current node.

After creating a file, compile the file with following command…

$ elixirc mynode.ex

Make sure you are in the same directory of the file mynode.ex is present.

This will create a file called Elixir.MyNode.beam . We are going to share this file across the different nodes.

Nodes Creation

Node 1

Node creation is so easy. Open your terminal and type following command. Make sure that you are in the same directory of the file Elixir.MyNode.beam is present because we are connecting to other nodes from this node and sharing the beam file we have created so far with other nodes.

$ iex --sname hello

After running the above command you will see something like the following…

Erlang/OTP 19 [erts-8.0.2] [source-753b9b9] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.4.0) - press Ctrl+C to exit (type h() ENTER for help)

This created a node with name hello and hostname blackode . Your hostname might be different from mine.

Here, your regular iex(1)> prompt has changed into some thing like iex(hello@blackode)1>

Now you are inside the node hello@blackode blackode is the host name or computer name.

Node 2

Lets create the another node with name hi from different directory of the file mynode.ex exists. For clear understanding of the nodes, be in different directory. You need not to be in the same directory of the file Elixir.MyNode.beam is exists.

How ever, you can still access that module by sharing this from the node hello@blackode . Soon you will see this coming into action. By now, you create another node with a command…

$ iex --sname hi

So far, you have created a module MyNode and the nodes hello@blackode hi@blackode .

Now go to the terminal with node hello@blackode and call the function hello inside the MyNode module .

iex(hello@blackode)1> MyNode.hello
hi !! hello@blackode

This function greets you with hi !! hello@blackode . Here hello@blackode is the node from where you are calling this function.

Now, you call the same function from the node hi@blackode

iex(hi@blackode)1> MyNode.hello
** (UndefinedFunctionError) function MyNode.hello/0 is undefined (module MyNode is not available)

This is because we have just created the nodes but we have not established a connection between them. Lets do that…

Node Connections

Go to the node hello@blackode and run Node.connect :hi@blackode . You should see the output as true which indicates you are connected to the node hi@blackode .

iex(hello@blackode)2> Node.connect :hi@blackode

You can check the connected nodes with function Node.list

iex(hello@blackode)3> Node.list

This indicates you are connected with the node hi@blackode .

Loading Elixir Modules to Node

Now the only step remaining is loading module. You can load the modules to another node by using the function nl

def nl(nodes \\ Node.list(), module)

Deploys a given module’s BEAM code to a list of nodes.

This function is useful for development and debugging when you have code that has been compiled or updated locally that you want to run on other nodes.

The node list defaults to a list of all connected nodes.

Returns {:error, :nofile}if the object code (i.e. “.beam” file) for the module could not be found locally.

The above explanation is copied from the original docs for giving you the more clarity about what this function does.

You just do nl modulename to share with all the connected nodes or else you pass the list with nodes like nl [:node1,:node2...],modulename .

Now lets share our MyNode module with all the connected nodes. So, our function call will be nl MyNode

iex(hello@blackode)4> nl MyNode
{:ok, [{:hi@blackode, :loaded, MyNode}]}

Now this module is loaded inside the node hi@blackode . Now you can call the function MyNode.hello from the node hi@blackode . This module is now accessible from all the connected nodes. Lets check whether the module is loaded or not.

Go to the node hi@blackode and run

iex(hi@blackode)1> MyNode.hello
hi !! hi@blackode

BOOM!! You have successfully deployed the module. Is this hard?

That’s all about loading modules from one node to the other. You can do more things with nodes. I think this will be the basics for playing with nodes in elixir.

Happy Coding!!

love to share….