N2O Erlang Processes are instantiated and run by Web Server. Depending on Web Server endpoint bindings you can specify module for HTTP requests handling.

N2O comes with three endpoint handlers for each Web Server supported. However you are not required to use any of these. You can implement your own endpoint handlers, e.g. for using with Meteor.js or Angular.js and providing Erlang back-end event streaming from server-side. Here is an example of using HTTP, WebSocket and REST endpoint handlers with Cowboy Web Server.

{"/rest/:resource", rest_cowboy, []}, {"/rest/:resource/:id", rest_cowboy, []}, {"/ws/[...]", n2o_stream, []}, {'_', n2o_cowboy, []}

HTML Pages over HTTP

This handler is used for serving initial dynamic HTML page. In case you are serving static HTML content this handler is not included into the running stack. n2o_cowboy is a default HTML page handler.

On initial page load n2o_document:run of page document endpoint is started. During its execution wf_render:render proceeds by calling Module:main selected by the routing handler.

JavaScript Events over WebSocket

JavaScript handler shares the same router information as the HTML handler because during its initial phase the same chain of N2O handlers is called.

This handler knows how to deal with XHR and WebSocket requests. n2o_stream is a default JavaScript event handler based on Bullet library created by Loïc Hoguin, optimized and refined.

You can send several types of events directly from JavaScript using various protocols. E.g. you may need to use client protocol:

JavaScript> ws.send(enc(tuple(atom('client'), tuple(atom('phone_auth'),bin("+380..")))));

And catch this event at Erlang side:

event({client,{phone_auth,Phone}}) -> io:format("Phone: ~p~n",[Phone]).

You can also send direct messages to event/1, but use it carefully because it may violate security rules.

> ws.send(enc(tuple(atom('direct'),atom('init'))));

With catching at Erlang side:

event(init) -> io:format("Init called~n").


REST handler’s request context initialization differs for the one used by HTML and JavaScript handlers. N2O handler chains are not applied to REST requests. rest_cowboy is a default REST handler.

{"/rest/:resource", rest_cowboy, []}, {"/rest/:resource/:id", rest_cowboy, []},
Listing 1: users.erl

-module(users). -behaviour(rest). -compile({parse_transform, rest}). -include("users.hrl"). -export(?REST_API). -rest_record(user). init() -> ets:new(users, [public, named_table, {keypos, #user.id}]). populate(Users) -> ets:insert(users, Users). exists(Id) -> ets:member(users, wf:to_list(Id)). get() -> ets:tab2list(users). get(Id) -> [User] = ets:lookup(users, wf:to_list(Id)), User. delete(Id) -> ets:delete(users, wf:to_list(Id)). post(#user{} = User) -> ets:insert(users, User); post(Data) -> post(from_json(Data, #user{})).

To add users to in-memory storage perform POST requests:

curl -i -X POST -d "id=vlad" localhost:8000/rest/users curl -i -X POST -d "id=doxtop" localhost:8000/rest/users curl -i -X GET localhost:8000/rest/users curl -i -X PUT -d "id=5HT" localhost:8000/rest/users/vlad curl -i -X GET localhost:8000/rest/users/5HT curl -i -X DELETE localhost:8000/rest/users/5HT


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