Erlang is popular because of its lightweight processes, actors and working rightly in distributed system. Elixir brings that on Lets look
spawn just simply spawn a compiler specific process and return PID, for letter communication and control.
A more mature example looks like
So above example will respond to every message that is sent to it, but only once since it is done with its job. To keep this alive we need to make a recursive call to it. Do not worry about stack overflow as erlang internally does Tail recursion optimisation, it discards dead terms. Look here for details.
Program above accepts any message and discard it, returns “HELLO” everytime. We can use elixir’s matchers here as well to respond to matched message, lets look
Now we will implement a router that routes a message to end route through intermediate routers. To implement this we need to implement router module, that just takes a message and route to next in given array of routers.
Let’s get started
okay so we are done with how our initial router will look like. Now we will implement a messanger, simply put a wrapper that creates router node and send message to very first route, Let’s have a look
Good stuff so far!. Let’s execute and see what happens