Skip to main content

Nitrogen Web Framework

Nitrogen is a powerful web framework built using the Erlang programming language.

It provides developers with a flexible and efficient way to build web applications. In this tutorial, we will explore the history, features, and examples of Nitrogen.

History of Nitrogen

Nitrogen was created by Rusty Klophaus in 2009. It was initially developed as a way to simplify the process of building web applications in Erlang. Over the years, Nitrogen has gained popularity due to its simplicity, scalability, and performance.

Features of Nitrogen

  1. Event-driven programming: Nitrogen follows an event-driven programming model, allowing developers to handle user interactions and server-side events easily. This makes it ideal for building real-time web applications.

  2. Dynamic HTML generation: Nitrogen makes it easy to generate dynamic HTML content using Erlang's powerful pattern matching and concurrency features. It provides a set of high-level abstractions that simplify the process of rendering HTML templates and handling form submissions.

    -export([render_page/1]).

    render_page(Req) ->
    Name = nitrogen_req:post_arg("name", Req),
    Age = nitrogen_req:post_arg("age", Req),
    {ok, #template{file="my_template.html", bindings=[
    {name, Name},
    {age, Age}
    ]}}.

    In this example, the render_page function receives a Req argument, which represents the HTTP request. It retrieves the values of the name and age fields from the request's POST data using the nitrogen_req:post_arg/2 function. It then generates an HTML template called my_template.html with the name and age variables bound to their respective values.

  3. Built-in AJAX support: Nitrogen provides built-in support for asynchronous JavaScript and XML (AJAX) requests. This allows developers to build interactive web applications that update their content dynamically without requiring a full page reload.

    -export([handle_event/2]).

    handle_event({ajax, _}, State) ->
    NewState = do_something(State),
    {ok, [], [{response, #refresh{}}], NewState}.

    In this example, the handle_event function handles an AJAX event. It performs some operation on the State data, updates it, and returns a response that instructs Nitrogen to refresh the page.

  4. Support for Comet and WebSockets: Nitrogen supports Comet and WebSockets, enabling developers to build real-time web applications that push server-side events to the client.

    -export([handle_event/2]).

    handle_event({websocket, _}, State) ->
    NewState = do_something(State),
    {ok, [], [{response, #refresh{}}], NewState}.

    In this example, the handle_event function handles a WebSocket event. It performs some operation on the State data, updates it, and returns a response that instructs Nitrogen to refresh the page.

  5. Security features: Nitrogen includes several security features out-of-the-box, such as protection against cross-site scripting (XSS) and cross-site request forgery (CSRF) attacks. It also provides easy-to-use APIs for handling user authentication and authorization.

Examples of Nitrogen

  1. Hello World: Let's start with a simple "Hello World" example.

    -module(hello_world).
    -export([start/0]).

    start() ->
    wf:start(),
    wf:wire(#button{
    id=hello_button,
    text="Click me",
    postback=hello
    }),
    wf:update(hello_div, [{body, "Hello, World!"}]).

    hello(postback, _) ->
    wf:update(hello_div, [{body, "Hello, Nitrogen!"}]).

    In this example, we define a module called hello_world with a start/0 function. The start/0 function starts the Nitrogen web server using the wf:start/0 function. It then creates a button with the ID hello_button and text "Click me" using the wf:wire/1 function. When the button is clicked, it triggers a postback event, which is handled by the hello/2 function. The hello/2 function updates the content of the hello_div element to display "Hello, Nitrogen!".

  2. Todo List: Let's create a simple todo list application.

    -module(todo_list).
    -export([start/0]).

    start() ->
    wf:start(),
    wf:wire(#button{
    id=add_button,
    text="Add",
    postback=add_todo
    }),
    wf:update(todo_list_div, [{body, []}]).

    add_todo(postback, _) ->
    Title = wf:q(post_arg, title),
    wf:update(todo_list_div, [{body, [Title | wf:q(todo_list_div, body)]}]).

    remove_todo(postback, Todo) ->
    wf:update(todo_list_div, [{body, lists:delete(Todo, wf:q(todo_list_div, body))}]).

    update_todo(postback, {OldTodo, NewTitle}) ->
    wf:update(todo_list_div, [{body, lists:substitute(OldTodo, NewTitle, wf:q(todo_list_div, body))}]).

    In this example, we define a module called todo_list with a start/0 function. The start/0 function starts the Nitrogen web server using the wf:start/0 function. It then creates an "Add" button, which triggers a postback event handled by the add_todo/2 function. The add_todo/2 function retrieves the value of the title field from the request's POST data and adds it to the todo list. The todo list is displayed in the todo_list_div element. Additionally, we have remove_todo/2 and update_todo/2 functions to handle removing and updating todos respectively.

These examples demonstrate just a few of the many features and capabilities of the Nitrogen Erlang framework. With its event-driven programming model, dynamic HTML generation, built-in AJAX support, and security features, Nitrogen provides a solid foundation for building robust and scalable web applications.

To learn more about Nitrogen, you can visit the official website here.