To run N2O websites you need to install Erlang version 20 or higher. N2O works on Windows, Mac and Linux.

Kickstart Bootstrap

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.

$ git clone git:// $ cd n2o/samples $ ./mad deps compile plan repl

Now you can try: http://localhost:8000.

On Linux you should do at first:

$ sudo apt-get install inotify-tools

Application Template

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:

Listing 1: sample.erl

-module(sample). -behaviour(supervisor). -behaviour(application). -export([init/1, start/2, stop/1, main/1]). main(A) -> mad:main(A). start(_,_) -> supervisor:start_link({local,review},review,[]). stop(_) -> ok. init([]) -> { ok, { { one_for_one, 5, 100 }, [spec()] } }. spec() -> ranch:child_spec(http, 100, ranch_tcp, port(), cowboy_protocol, env()). port() -> [ { port, wf:config(n2o,port,8000) } ]. env() -> [ { env, [ { dispatch, points() } ] } ]. static() -> { dir, "apps/sample/priv/static", mime() }. n2o() -> { dir, "deps/n2o/priv", mime() }. mime() -> [ { mimetypes, cow_mimetypes, all } ]. points() -> cowboy_router:compile([{'_', [ { "/static/[...]", n2o_static, static()}, { "/n2o/[...]", n2o_static, n2o()}, { "/ws/[...]", n2o_stream, []}, { '_', n2o_cowboy, []} ]}]).

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.

Companion Dependencies

For raw N2O use with BERT message formatter you need only one N2O dependecy, but if you want to use DTL templates, JSON message formatter, SHEN JavaScript Compiler or NITRO Nitrogen DSL you can plug all of them separately. 2.9 2.9 2.9 2.9 1.5 1.5 1.5 1.5


Listing 2: sys.config

[{n2o, [{port,8000}, {app,review}, {route,routes}, {mq,n2o_mq}, {json,jsone}, {log_modules,config}, {log_level,config}, {log_backend,n2o_log}, {session,n2o_session}, {origin,<<"*">>}, {bridge,n2o_cowboy}, {pickler,n2o_pickle}, {erroring,n2o_error}]}, {kvs, [{dba,store_mnesia}, {schema, [kvs_user, kvs_acl, kvs_feed, kvs_subscription ]} ]} ].


N2O uses two ports for WebSocket and HTTP connections.

wf:config(n2o,port,443) wf:config(n2o,websocket_port,443)

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:

<script> var transition = { pid: '', host: 'localhost', port: '443' }; </script>


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:

wf:info(index,"Message: ~p",[Message]),

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.

-module(config). -compile(export_all). log_level() -> info. log_modules() -> [ login, index ].

Message Queue

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.

Minimal Page

And then add a minimal index.erl page:

Listing 3: index.erl

-module(index). -compile(export_all). -include_lib("nitro/include/nitro.hrl"). main() -> #span{body="Hello"}.


Events | Privacy Policy | Feedback | Brandbook
Copyright © 2005–2018 Synrc Research Center s.r.o.