{"id":6596,"date":"2014-03-26T12:10:14","date_gmt":"2014-03-26T09:10:14","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=6596"},"modified":"2020-12-28T19:25:22","modified_gmt":"2020-12-28T16:25:22","slug":"interconnecting-elixir-nodes","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/interconnecting-elixir-nodes\/","title":{"rendered":"Interconnecting Elixir nodes"},"content":{"rendered":"Elixir is a functional language built on top of Erlang VM. Therefore it uses Erlang mechanism for communication with nodes. Erlang has one simple rule: everyone connects to everyone. It provides a simple authentication mechanism by tokens called cookie.\r\nIn this article I want to show how to use remote shell for interconnection between Elixir nodes.\r\n<h2>Start elixir shell<\/h2>\r\n<pre class=\"lang:default decode:true \">iex --sname node --cookie secret_token -S mix\r\n<\/pre>\r\nWe use sname and cookie parameters to specify short name and cookie.\r\nMake sure that settings were applied successfully:\r\n<pre class=\"lang:default decode:true \">iex(node@noname)&gt; Node.self\r\n:node@noname\r\niex(node@noname)&gt; Node.get_cookie\r\n:secret_token\r\n<\/pre>\r\nStart another shell, but without cookie:\r\n<pre class=\"lang:default decode:true \">iex --sname node2\r\n<\/pre>\r\nIn this shell we\u2019ll use Node module to set cookie. We can try to ping our first node:\r\n<pre class=\"lang:default decode:true \">iex(node2@noname)&gt; :net_adm.ping(:node@noname)\r\n:pang\r\n<\/pre>\r\n:pang atom was returned because cookies weren&#8217;t matched and connection wasn&#8217;t established. Setup identical cookie for the second node and try again:\r\n<pre class=\"lang:default decode:true \">iex(node2@noname)&gt; Node.set_cookie :secret_token\r\ntrue\r\niex(node2@noname)&gt; :net_adm.ping(:node@noname)\r\n:pong\r\n<\/pre>\r\n<h2>Connect to remote shell using Job Control Mode JCL<\/h2>\r\nActivate this mode using Control + G:\r\n<pre class=\"lang:default decode:true \">iex(node2@noname)&gt;\r\nUser switch command\r\n --&gt;\r\nDisplay help message:\r\niex(node2@noname)&gt;\r\nUser switch command\r\n  --&gt; h\r\n  c [nn]            - connect to job\r\n  i [nn]            - interrupt job\r\n  k [nn]            - kill job\r\n  j                 - list all jobs\r\n  s [shell]         - start local shell\r\n  r [node [shell]]  - start remote shell\r\n  q                 - quit erlang\r\n  ? | h             - this message\r\n<\/pre>\r\nShow list of jobs:\r\n<pre class=\"lang:default decode:true \">--&gt; j\r\n   1* {erlang,apply,[#Fun,[]]}\r\n<\/pre>\r\nWe have only one job which is our current shell. Start remote Elixir shell:\r\n<pre class=\"lang:default decode:true \"> --&gt; r node@noname 'Elixir.IEx'\r\n<\/pre>\r\nVery important to specify the second parameter as Elixir.IEx module, otherwise Erlang shell will be started instead of Elixir.\r\n\r\nMake sure that the shell was started:\r\n<pre class=\"lang:default decode:true \"> --&gt; j\r\n   1  {erlang,apply,[#Fun,[]]}\r\n   2* {node@noname,'Elixir.IEx',start,[]}\r\n<\/pre>\r\nConnect to job number 2 which is our first node:\r\n<pre class=\"lang:default decode:true \"> --&gt; c 2\r\nEshell V5.10.2  (abort with ^G)\r\niex(node@noname)&gt;\r\n<\/pre>\r\nSucceed! Make sure that we were connected to the right node:\r\n<pre class=\"lang:default decode:true \">iex(node@noname)&gt; Node.self\r\n:node@noname\r\n<\/pre>\r\nNow we\u2019re able to monitor, change configuration, execute any code in the connected node.\r\n<h2>Disconnect node<\/h2>\r\nOnce all the work was done, don\u2019t forget to disconnect remote shell in the right way. Press Control + G:\r\n<pre class=\"lang:default decode:true \">User switch command\r\n --&gt; j\r\n   1  {erlang,apply,[#Fun,[]]}\r\n   2* {node@noname,shell,start,[]}\r\n<\/pre>\r\nKill job number 2 and make sure that shell was disconnected\r\n<pre class=\"lang:default decode:true \">k 2\r\n --&gt; j\r\n   1  {erlang,apply,[#Fun,[]]}\r\n<\/pre>\r\nAnd switch back to second node:\r\n<pre class=\"lang:default decode:true \"> --&gt; c 1\r\niex(node2@noname)&gt; Node.self\r\n:node2@noname\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Elixir is a functional language built on top of Erlang VM. Therefore it uses Erlang mechanism for communication with nodes. Erlang has one simple rule: everyone connects to everyone. It provides a simple authentication mechanism by tokens called cookie. In this article I want to show how to use remote shell for interconnection between Elixir&#8230;<\/p>\n","protected":false},"author":44,"featured_media":9458,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Olexander Paladiy"],"class_list":["post-6596","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/themes\/railsware\/vendors\/images\/article-thumbnail-default.jpg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=6596"}],"version-history":[{"count":54,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6596\/revisions"}],"predecessor-version":[{"id":13335,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/6596\/revisions\/13335"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/9458"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=6596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=6596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=6596"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=6596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}