To run N2O websites you need to install Erlang version 20 or higher. N2O works on Windows, Mac and Linux.
To try N2O you only need to fetch it from Github and build. We don’t use fancy scripts, so building process is OTP compatible: bootstrap site is bundled as an Erlang release.
Now you can try: http://localhost:8000.
On Linux you should do at first:
If you want to start using N2O inside your application, you can use Cowboy dispatch parameter for passing HTTP, REST, WebSocket and Static N2O endpoints:
While Listing 1 is a little bit criptic we want to say that N2O intentionally not introduced here any syntax sugar. For any Erlang application you need to create application and supervisor behavior modules which we combined in the same Erlang file for simplicity.
Cowboy routing rules also leaved as is. We’d better to leave our efforts for making N2O protocol and N2O internals simplier. Here we can’t fix a much. Just use this as template for bootstrapping N2O based applications.
N2O uses two ports for WebSocket and HTTP connections.
If you use server pages mode N2O will render HTML with nessesary ports values. For single page application mode you should redefine these ports inside the template:
In app setting you should place the name of your OTP application that will be treated by N2O and NITRO as a source for templates and other static data with code:priv_dir.
Setting route is a place for the name of Erlang module where resides mappings from URL to page modules.
N2O supports logging API and you can plug different logging module. It ships with n2o_io and n2o_log modules which you can set in the log_backend option. This is how logging looks like in N2O:
First argument is a module from which function is being called. By using this N2O can filter all log messages with special filter settled with log_modules option. It says in which Erlang module function log_modules/0 exists that returns allowed Erlang modules to log. Option log_level which specified in a similar way, it specifies the module with function log_level/0 that could return one of none, error, warning or info atom values which means different error log levels.
In mq settings you should place the name of Erlang module which supports message queue API. By default N2O provides n2o_mq module.
With formatter option you may set the WebSocket channel termination formatter such as bert or json. If you will select json as formatter you may want also to chose a json encoder in json option. By default in n2o enabled json formatter and jsone encoder. The main reason is that jsone is written in pure erlang idiomatic code and is ready to run on LING without C NIF linkage. But you may want to switch to jsonx on BEAM or whatever.
And then add a minimal index.erl page: