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. Refer Erlang Efficiency Guide 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
That’s about processes in elixir.
Happy Coding :)
About The Author
I am Pankaj Baagwan, a System Design Architect. A Computer Scientist by heart, process enthusiast, and open source author/contributor/writer. Advocates Karma. Love working with cutting edge, fascinating, open source technologies.
To consultPankaj Bagwan on System Design, Cyber Security and Application Development, SEO and SMO, please reach out at me[at]bagwanpankaj[dot]com
For promotion/advertisement of your services and products on this blog, please reach out at me[at]bagwanpankaj[dot]com