Connecting erlang nodes

Tuesday, 17th May, 2011

In order to connect to each other, erlang nodes each need a name, they need to share a secret cookie, and if they’re to communicate over the internet, they need access to ports.

The secret cookie can be either set at runtime (as in the examples below), or in each user’s .erlang.cookie file.

For local connection

On the same machine or subnet, each node just needs a short name:

$ erl -sname chico -setcookie marx
...
(chico@localhost)1>

For internet connection

Each node must have the following ports available:

  • port 4369, used by epmd (the Erlang Port Mapper Daemon, not Erick and Parrish Making Dollars), must be open for both TCP and UDP (n.b.: this is a default).
  • another port or range of ports for the erlang nodes themselves. These nodes can be set at run time using the -kernel, inet_dist_listen_min and inet_dist_listen_max flags.

Each node must also use a full name, with either a domain or an IP address:

$ erl  -name chico@brothers.org  -setcookie longrandomstring  -kernel inet_dist_listen_min 9000  inet_dist_listen_max 9005
(chico@brothers.org)1>

Connecting

Erlang nodes are gregarious: as soon as nodes find out about each other, they connect. An easy way to say hello is “ping”:

(chico@localhost)1> nodes().
[]
(chico@localhost)2> net_adm:ping(groucho@localhost).
pong
(chico@localhost)3> nodes().                        
[groucho@localhost]
(chico@localhost)4> ^g
User switch command
 --> r groucho@localhost
 --> c
Eshell V5.7.2  (abort with ^G)
(groucho@localhost)1> 

You can skip the connection palaver by using the -remsh flag at startup:

$ erl -sname chico -setcookie marx -remsh groucho@localhost
...
(groucho@localhost)1>

etc

Introspection GUIs like AppMon and Pman can access any connected node (see the Nodes menu in the toolbar).

Please find below wave.erl, an erlang script for reading and writing .wav audio files. I am releasing it under the ISC license.

The script exports functions read/1, write/3 and write/2, and the wave record:

Read the rest of this entry »

When using a named function (i.e., rather than an anonymous function) with lists:map/2, it is necessary to use the form fun module:function/arity. For example, the module [1] will raise the error [2]:

[1]

-module(maptest). 
-export([test/0]). 
-export([double/1]). 

double(N) -> 
    2 * N. 

test() -> 
    List = [1,2,3,4,5], 
    lists:map(double, List).

[2]

1> c(maptest). 
{ok,maptest} 
2> maptest:test(). 
** exception error: bad function double 
     in function  lists:map/2

Module [3] works:

[3]

-module(maptest). 
-export([test/0]). 
%%  -export([double/1]).  %% not necessary 

double(N) -> 
    2 * N. 

test() -> 
    List = [1,2,3,4,5], 
    lists:map(fun double/1, List). 

[4]

1> c(maptest).
{ok,maptest}
2> maptest:test().
[2,4,6,8,10]

So the first argument of map — whether the function is named or anonymous — always starts with the keyword fun:

[5]

lists:map(fun(X) -> 2 * X end, List).

As an aside, it is not necessary to export the function in order to make it accessible to map (cf. [3]).

This is a summary of recent discussion on the erlang-programming-book Google group.

This is not really a post, more like a braindump. I need somewhere to offload all this so I can go to bed. I apologise for lack of context.

OK some context: I’m looking for a chat application I can use within a django-based website (and if I can’t find one, girding my loins to write one). Irrelevantly (or so I’d thought until a few minutes ago) I’m learning Haskell, and finding out about functional programming.

Haskell:

Erlang: