Odd Red Dog

Easy IPC between Erlang and Ruby using BERT

29 September 2013

An easy way to have Erlang and Ruby communicate together is to use BERT. BERT boils down to providing languages a serializable format that is compatible with the Erlang External Term Format. Erlang uses its external term format to implement inter-node communication (see Distributed Erlang).

From Erlang itself (without any add-on libraries) one still needs to comply to the specifics of the BERT protocol. Those specifics are there to facilitate the back-and-forth between Erlang and other languages when exchanging data structures.

For example, sending an Erlang property list to Ruby using BERT won't end up creating a Ruby Hash on its own. Instead, a tuple such as {bert, dict, [{key1, <<"Value1">>}]} must be used.

Luckily, there are BERT libraries for both Ruby and Erlang.

Coding data between Ruby and Erlang is easily done using BERT.encode(decoded) and BERT.decode(encoded). Hint: if you need to find what the length of the encoded data is so that you can send it back to an Erlang port or socket, use BERT::bytesize() like:

output.write([reply.bytesize()].pack("N"))
output.write(reply)
output.flush()

BERT then provides an easy to use protocol that can be used over a communication channel such as a socket or an Erlang port. You can see a basic example that uses a port here (taken from work in progress).