{"id":4303,"date":"2014-11-12T09:00:42","date_gmt":"2014-11-12T11:00:42","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=4303"},"modified":"2015-06-01T12:24:28","modified_gmt":"2015-06-01T15:24:28","slug":"converting-erlang-code-into-elixir","status":"publish","type":"post","link":"http:\/\/blog.plataformatec.com.br\/2014\/11\/converting-erlang-code-into-elixir\/","title":{"rendered":"Converting Erlang code into Elixir"},"content":{"rendered":"

When you are new to any language, you probably want to run some existing code just to see how it works. Achieving success<\/a> while trying new things is important, because it helps fueling your interest.<\/p>\n

The number of code examples in Elixir is increasing, but sometimes you will have to read some Erlang code. Recently, I wanted to play a little bit with Cowboy HTTP Server, which is written in Erlang. The Cowboy repo has a lot of small examples<\/a> presenting the features which is provided by it. When I tried to convert one of them to Elixir, it wasn’t as simple as I expected, since I’m not so familiarized with the language yet.<\/p>\n

When converting, you may get into some misleading code that will not work as you expected at first. So, I’m going to present a transcoding of Cowboy WebSocket server example<\/a> from Erlang to Elixir, so that you can learn some of the details that exists in the process of porting Erlang code into Elixir code.<\/p>\n

This will not be a tutorial explaining how that Cowboy example works, it’s just about how to convert it to Elixir. Also, I’m not going to show how it could be done in idiomatic Elixir, the goal here is to translate Erlang code into Elixir the simplest way possible.<\/p>\n

So let’s start!<\/p>\n

Creating the project<\/h2>\n

Create a project called ws_cowboy<\/code> with the following command:<\/p>\n

\nmix new ws_cowboy\ncd ws_cowboy\n<\/pre>\n

After that we are going to change\/create 4 files:<\/p>\n