MyNixOS website logo
Description

Send messages to an Erlang node using Haskell.

Please see the README on GitHub at https://github.com/Szetty/erlang-ffi#readme

IT IS A MODIFIED VERSION OF THE ORIGINAL erlang-ffi TO MAKE IT WORK WITH NEW AND MODERN STACK.

erlang-ffi

Should work with with OTP version < 23 (have tried only with OTP 22 and 23).

Speaks the Erlang network protocol and impersonates an Erlang node on the network. Fully capable of bi-directional communication with Erlang. Erlang types are, as far as reasonable, mapped to Haskell types. Messages to Erlang are just function calls in Haskell, and messages from Erlang are delivered to MVars.

Short example

We introduce the erlang library through a simple working example. We are going to write a simple ''Hello, Erlang!'' program.

Lets start with the Haskell code. Here we initialize the ffi with createSelf, and create a mailbox with createMBox. Then we send our greeting with our pid to a registered process p running on Erlang node [email protected]. We expect an answer, so we get it from the mailbox and print it out to the standard output.

import Foreign.Erlang

main :: IO ()
main = do
  self <- createSelf "haskell@localhost"
  mbox <- createMBox self
  mboxSend mbox (Long "erl" "127.0.0.1") (Right "p") (mboxSelf mbox, "Hello, Erlang!")
  answer <- mboxRecv mbox
  putStrLn ("Received: " ++ fromErlang answer)

Alright, that wasn't so hard! Lets continue with the Erlang side. We are not going to do any fancy thing here neither. We register a process that receives a message and replies to it.

-module(hello).
-export([main/0]).

main() ->
    P = spawn(fun f/0),
    register(p, P).

f() ->
    receive
        {Hs, Msg} ->
            io:format("Received ~p~n", [Msg]),
            Hs ! "Hello, Haskell!"
    end.

Lets start up the engines! We are ready to compile and run our programs.

$ ghc Hello.hs
[1 of 1] Compiling Main             ( Hello.hs, Hello.o )
Linking Hello ...
$ erlc hello.erl

Firstly, we start an Erlang node:

$ erl -name "[email protected]"
Eshell V6.2  (abort with ^G)
([email protected])1> l(hello).
{module,hello}
([email protected])2> hello:main().
true

Erlang node is started, and the process is registered as well. Now start the Haskell program:

./Hello 
Received: Hello, Haskell!

Taking a look at the Erlang shell, we see that the message is successfully delivered:

([email protected])3> Received "Hello, Erlang!"
Metadata

Version

1.0.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows