So, I saw and played with node.js, and one of the things that impressed me was the nice clean website.
This is just a tongue in cheek clone of the node.js website but focused on Erlang.
An example of a web server written in Erlang which responds with “Hello World” for every request. Uses misultin as the web server library.
-module(example).
-export([start/0]).
start() ->
code:add_patha(“misultin/ebin”), % web-server library
misultin:start_link([{loop, fun handle_http/1},{ip, “127.0.0.1”},{port, 8124}]),
io:format(“Server running at http://127.0.0.1:8124/~n”),
receive _ -> ok end.
handle_http(Req) ->
Req:respond(200, [{“Content-Type”, ”text/plain”}], ”Hello World”).
To run the server, put it into a file called example.erl, then compile and run it.
erlc example.erl
erl -s example
Here is an example of a simple TCP server that listens on port 8124 and echoes whatever you send it.
-module(example).
-export([start/0]).
start() ->
{ok, LSocket} = gen_tcp:listen(8124, [binary, {packet, 0}, {active, false}, {reuseaddr, true}]),
accept(LSocket).
accept(LSocket) ->
{ok, Socket} = gen_tcp:accept(LSocket),
spawn(fun() -> loop(Socket) end),
gen_tcp:send(Socket, ”Echo server\r\n”),
accept(LSocket).
loop(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} -> gen_tcp:send(Socket, Data), loop(Socket)
{error, closed} -> ok
end.
Download
git repo, website
Stable: R13B04 (win32)
Unstable: R14B01 (win32)
Historical: versions, docs
Build
Erlang is tested and used on Linux, Macintosh, Solaris, Windows (Natively), as well as FreeBSD and OpenBSD.
About
Erlang’s goal is to provide an easy way to build scalable, distributed, highly concurrent, functional programs. In the “hello world” web server example above, many client connections can be handled concurrently. Erlang spawns a new Erlang process to handle each connection, these are ultra-light weight processes designed with high concurrency in mind, they are not to be confused with OS processes.
This is in contrast to today’s more common concurrency model where OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use. See: this and this. Erlang will show much better memory efficiency under high-loads than systems which allocate 2mb thread stacks for each connection. Furthermore, users of Erlang are free from worries of dead-locking the process—there are no locks. Almost no function in Erlang directly performs I/O, so the process never blocks. Because nothing blocks, less-than-expert programmers are able to develop fast systems.
Erlang was designed for concurrency - having multiple tasks running simultaneously - from the ground up it was a central concern when the language was designed. Its built-in support for concurrency, which uses the process concept and message passing to get a clean separation between tasks, allows us to create fault tolerant architectures and fully utilize the multi-core hardware and distributed systems that is available to us today.
TCP handling in Erlang is easy, so by extension processing HTTP becomes straightforward. Erlang’s HTTP libraries have grown out of the authors experiences developing and working with web servers. For example, streaming data through most web frameworks is impossible. Erlang attempts to correct these problems via its process oriented nature and support for concurrency. Having the freedom to spawn a long-lived process to communicate with a single client via comet or websockets makes Erlang a delight to work with.
But what about multiple-processor concurrency? Aren’t threads necessary to scale programs to multi-core computers? Processes are necessary to scale to multi-core computers, not memory-sharing threads. The fundamentals of scalable systems are fast networking and non-blocking design—the rest is message passing. Erlang has a -smp option, which allows it to take full advantage of multiple processors with near linear scaling, and has support for distributed process built right in.
See Also:
Links
Contributing
Erlang has a detailed contribution page, but I will summarize it here. Please read the entire thing before attempting to get patches mainlined.
- Get a git repo somewhere publicly accessible, use proper git settings.
- Send an email to the list with the git repo location and branch, so discussion can start on your patch.
More detailed/specific edition:
- Setup a github account (they are free)
- Fork the official repo from the dev branch.
- Set up consistent user information before starting with your real name and working email on all your computers. (git config —global user.name “Your Name Comes Here”, git config —global user.email you@yourdomain.example.com)
- If you got this far, you really need to read the contribution page on the wiki.