Building Web Apps with MongoDB, Node.js and Angular.js

I became an iPhone developer the moment I realized that the iPhone is not just a pretty and expensive phone; but rather that it is actually a networked Unix machine that fits in your pocket. The fact that it could (sometimes) make phone calls was incidental. From the very beginning it was a personal information device that I could use to find, consume and visualize all sorts of data.

Visualizing and communicating data is the reason that I've become fascinated by recent advances web app architectures. The current architectures and frameworks will continue to be useful for a long long time. However smart interactive clients, talking to real time servers, managing document stores, will allow us to build all kinds of exciting interactive 'real time' apps faster and easier than ever before. By smart interactive clients I mean both native and browser based apps. On the native side, I prefer (writing) iOS and Mac OS X apps. For browser based, today that implies lots of JavaScript to manipulate a single or a small number of pages.

I have at least two projects I want to build using this architecture. The first is a side project based around some ideas that interest me and the second may have more commercial value. Their details are not important right now though I plan on discussing the first at length in future posts as I learn more.

I've built prototypes for both of these project using Ruby on Rails (RoR) which is great and you should keep using it if you know and love it. In particular RoR is amazing because of the language, the ecosystem of gems and tools, and its explicit and implicit conventions, assumptions and opinions. However, I'm finding that RoR may not be a perfect fit for these new types of web apps. Let me explain some of my reasoning.

MongoDB

I built the two prototypes using MySQL and PostgreSQL. Those databases worked great and ActiveRecord/ActiveModel made creating the schemas easy. However, I'm drawn to the document model since it seems natural to work with data structures that contain or own other data structures rather than having to rebuild them from independent tables all the time. Document stores are flexible, can be fast in the right circumstances and I believe require less support from an ORM/ODM.

Choosing a document/NoSQL database is a different question as there are many choices (MongoDB, CouchDB, etc.) and various articles try to outline the differences. After looking them over MongoDB seems like a great default choice for me. Its fast, flexible and easy to query making it great to develop with. As I learn more and as my specific needs evolve I may swap out part or all of MongoDB but for now I could not be happier with it and with the support from 10Gen.

Angular.js

jQuery is great for DOM manipulation but highly interactive clients get complicated quickly and can benefit from the structure of an additional application framework. Lately BackBone.js is getting a lot of attention. I've also looked at SproutCore 2.0 and Batman.js and they too seem workable. However the one that felt easiest and cleanest is Angular.js.

Some things I like about it are that the "templates" are plain html, bindings and controllers are dead simple, it supports dependency injection, xhr requests are straight forward and it has a strong testing culture. Its development is being supported by Google and I expect to hear more from other developers soon. I suggest you check it out and let me know what you think.

Node.js

Though Ruby on Rails is awesome, given that JavaScript is required for the front end and we are using a JavaScript/JSON friendly backend, having a different language and system in the server layer may not be ideal. In particular, since I feel there is less of a need for the infrastructure that Rails provides and there are benefits to going with a simpler faster framework/system.

Node.js allows you to write fast evented servers in JavaScript. It is single threaded but is event based and can handle multiple simultaneous connections. Additionally it is fast and has small memory requirements so you can run multiple instances if desired to improve capacity, throughput and CPU utilization. Evented programming is different and JavaScript has its quirks but together they make a really powerful combination.

Node.js has a growing ecosystem of modules and an active community. I highly recommend you give server side JavaScript a try.

Conclusion

I'm sure not all of these decisions will work out. I'll write more as I learn and figure things out but right now I plan to use:

I've already started using this combination of tools and am very excited about the potential and what I can build.

Thats it for now. Thanks for letting me outline my thought process and describe my experience with these projects. Next time I'll explain more about the first project and we'll start building it.