Build It with Nitrogen (Leanpub)

By @gumm

Web development has never been more challenging. The Internet is intensely competitive and treacherous. Site malfunction and downtime are unacceptable. Clients demand ever more features with ever tighter deadlines. Popular sites must scale to handle unpredictable traffic spikes.

Build It with Nitrogen: the Fast Off the Block Erlang Web Framework addresses these issues and more. It assumes minimal Linux or JavaScript skills. The easy to-read book guides the reader through seven hands-on projects. Each project builds on the last toward high-level competency. Readers learn Erlang as they go. ā€œNitrogen simplifies development of web applications, making simple things easy and difficult things manageable,ā€ says Zotonic CMS creator Marc Worrell.

ā€œThis book is ambitious,ā€ says Nitrogen creator Rusty Klophaus, ā€œIt covers not only Nitrogen and Erlang, but also a bit of OTP, databases, git, web design, and software engineering best practices. And somehow, it does this well by building up from simple concepts in a sort of ā€˜Socratic dialogueā€™ā€”if Socrates had a ā€˜California surferā€™ sense of humor.ā€


Build It with Nitrogen: the Fast Off the Block Erlang Web Framework guides web developers step-by-step through the construction of highly reliable web applications using Erlang and the Nitrogen Web Framework.

This easy-to-read book assumes minimal Linux or JavaScript skills; guides the reader through 7 hands-on projects. Each project builds on the last toward high-level competency. Readers learn Erlang as they go. Nitrogen simplifies development of web applications, making simple things easy and difficult things manageable.

Erlang delivers the high availability, massively scalable, soft real-time performance required by banking, e-commerce, computer telephony, and instant messaging applications.

What Youā€™ll Learn in Build It With Nitrogen

  • How to build basic CRUD interfaces
  • How to build highly dynamic interfaces
  • How to work with the templates and the DOM
  • How to work with logins and password hashing
  • How to use OTP basics with gen_servers and supervisors
  • How to use the databases: DETS, Mnesia, and PostgreSQL
  • How to interact with external APIs (in our case, retrieving stock quotes)
  • How to build your own behaviours and use those to produce Nitrogen pages more rapidly
  • How to build custom Nitrogen elements and convert those into plugin libraries
  • The basics of web application security
  • The basics of using other front-end frameworks (specifically Bootstrap)
  • How to avoid potential maintenance traps
  • The basics of Git
  • Learn Erlang along the way

https://builditwith.com/nitro

7 Likes

This comment is making me want to read this book :lol:

I believe @Maartz has already started it, so I will be looking forward to hearing his thoughts too! :smiley:

3 Likes

Haha! Thanks!

It was Lloydā€™s idea to do it as a dialog, and I think it really worked well.

I appreciate the plug!

2 Likes

Yeah still got the Book Club on devtalk. I need to continue reading it. So far so good I liked it!
Whenever I got some more time Iā€™ll go back to the book club.

3 Likes

Awesome! If you stumble across any issues or confusing bits, donā€™t hesitate to ask.

3 Likes

I bought and read this book. Great thing! Now Iā€™m making an e-commerce application using Nitrogen. By the way, as a build system I migrated to rebar3.
IMO, Nitrogen was ahead of the time when it was created and itā€™s very relevant now.
And conceptually, it eliminates the choice of the back end framework, the frontend framework, as well as an exchange protocol between them. So you can make the entire stack on Erlang, itā€™s very cool. And with less effort, it turns out almost React / Angular look&feel for customers thanks to dynamic content. Soon Iā€™ll see how it behaves in production, the current tests are promising))

4 Likes

Very awesome! So excited to hear about it. Iā€™ve been kicking around ideas for an e-commerce application of my own for a while now.

Regarding rebar3, Iā€™m actively working on some automated tools to migrate existing rebar2-based Nitrogen projects to rebar3 in preparation of Nitrogen 3. Itā€™s been tricky trying to retain backwards compatibility with the current rebar2-based projects, but Iā€™m almost there.

3 Likes

We did a e-commerce system with Nitrogen and Riak when I studied at Uppsala University. A collaboration with Klarna at that time.

Also some of the files. Remember I tried to update it but was missing something.

4 Likes

Iā€™m currently reading the sample of this book and the full version is on the way. :slight_smile:

In the chapter 3 (nitroBoard I), after generating the slim project nb I got the following error.

āžœ nb (ī‚  master)āœ˜ bin/nitrogen console

Exec: /Users/arif/.asdf/shims/../erts-12.2.1/bin/erlexec -boot_var RELTOOL_EXT_LIB /Users/arif/Practice/Erlang/testproj/nb/lib -sasl releases_dir "/Users/arif/Practice/Erlang/testproj/nb/releases" -boot /Users/arif/Practice/Erlang/testproj/nb/releases/2.4.0/nitrogen -mode interactive -config /Users/arif/Practice/Erlang/testproj/nb/etc/app.config -config /Users/arif/Practice/Erlang/testproj/nb/etc/simple_bridge.config -config /Users/arif/Practice/Erlang/testproj/nb/etc/sync.config -args_file /Users/arif/Practice/Erlang/testproj/nb/etc/vm.args -- console
Root: /Users/arif/.asdf/shims/..
bin/nitrogen: line 299: /Users/arif/.asdf/shims/../erts-12.2.1/bin/erlexec: No such file or directory
bin/nitrogen: line 299: exec: /Users/arif/.asdf/shims/../erts-12.2.1/bin/erlexec: cannot execute: No such file or directory

I have installed Erlang through asdf.

I assume from the error that nitrogen is looking for a directory named erts-12.2.1 somewehre in the ~/.asdf directory and isnā€™t finding it.

Edit: Currently, I re-generated the project using full release instead of the slim release to continue reading the book. :slight_smile:

3 Likes

Awesome! Glad to hear it!

Yeah, the slim release is looking for an erts-X.Y.Z directory in the erlang directory.

Glad to hear youā€™re able to continue plugging along with the full release. That should work for you for now. Iā€™m not familiar with asdf's conventions as a package manager, so Iā€™ll have to experiment, but I wonā€™t be spending too much time on it, as Nitrogen 3 is ā€œright around the corner,ā€ which drops rebar2, finally adopting rebar3/relx and the difference there is just tweaks to the default config file, rather than fully structural things.

If you have other questions, donā€™t hesitate to ask, and I appreciate you reading!

2 Likes

Thank you! :slight_smile:

Will the book update with Nitrogen 3?

2 Likes

I found the directory nitrogen was looking for. Itā€™s ~/.asdf/installs/erlang/24.2.1/erts-12.2.1.
Right now Iā€™m super tired. Tomorrow I will generate the slim version of the project and try to tell nitrogen where this directory is. I think editing a single file manually for the life of a project wonā€™t hurt.

3 Likes

Oh, it sure will be! Iā€™m going at this rather slowly as well, because Iā€™m rather dogmatic about retaining backwards compatibility (on top of having a day job and family and the usual).

But absolutely. My goal is to make sure that when N3 is released, that the current edition of the book does not break, while Iā€™ll take some time to update the book to ensure that the ā€œupdatedā€ version of the book more closely follows the N3 conventions (which are really more like a superset of the OTP conventions). At current, Nitrogen 2 kind of uses its own conventions out of the box and theyā€™re definitely not OTP standard conventions (the most obvious is the use of the site/ directory as a container for src, include, etc. instead of those directories existing in the root of the project directory).

3 Likes

Currently I made it work by adding erlang installation folder to the path, so that the Erlang executable from the installs/erlang/24.2.1/bin is used instead of the one from shims folder, and when the script is looking for the erts-12.2.1 folder under the parent directory of bin, it is finding it.

3 Likes

Excellent! That sounds about right. So glad you were able to figure it out!

3 Likes

Thank you @gumm! :slight_smile:
Iā€™m really enjoying this book.

3 Likes

I resumed the book.
Iā€™m following all the steps exactly as instructed. The Listing 3.18 (rr(visitors_db).) is giving me {error,nofile} instead of [visitor].

Edit: It worked. I think I was running it outside the site directory.

2 Likes

Oh, good catch. This bit is assuming that sync has been running to recompile it automatically.

However, if you exit the erlang VM, from the ~/nb directory:

make
cd site
erl -pa ebin

and it should work.

3 Likes

@gumm

After learning Nitrogen, I want to use it in some real/toy project as the web app and also for writing the back-end (RESTful APIs) of mobile apps.
Is writing APIs in Nitrogen as easy as writing a web app in it?

2 Likes

Yeah, writing a restful API is quite easy in Nitrogen (Nitrogen - Documentation).

The big thing is to: add this to your module:

An example of a simple GET api would be this.

Request: http://localhost:8000/userinfo?userid=12345

Anticipated Response:

{"name": "Joe", "email": "joe@example.com"}

Hereā€™s what that module could look like:

-module(userinfo).
-export([
   get/1
]).
-behaviour(nitrogen_rest).

get(_PathInfo) ->
    ID = wf:q(userid),
    case db_user:get(ID) of
        undefined ->
            {404, "Not Found"};
        User ->
            wf:json_encode(User)
    end.

In the above context, User would be a proplist that looks like like: [{name, <<"Joe">>}, {email, <<"joe@example.com">>}]

That said, if you need a really robust restful API, dipping directly into a cowboy handler would be recommended. But Nitrogen can handle most restful APIs.

3 Likes