If you purchased this ebook directly from oreilly.com, you have the following benefits:
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Preface
When Google released the first version of their V8 JavaScript engine in 2008, it felt like a hushed wave of excitement was rippling through the developer community. For the first time (the promise went), we would be able to program with JavaScript on both the client and the server: one language to rule them all. Web applications were already starting to become more desktop-like and ballooning in complexity, so the idea of reducing the number of language dependencies in favor of an open and transparent technology was seen as a way to allow for even more exciting and boundary-pushing applications.
Ryan Dahl was one of the developers who saw the new opportunity and wasted no time converting the non-blocking socket library he had written to the new V8 engine, resulting in the birth of Node.js. The technology he released has turned that original ripple of excitement into a major paradigm shift at a time when interest in responsive real-time applications is reaching a peak. Node.js is more than just a collection of socket functions; it provides a framework for asynchronous I/O that position it as the foundation of a whole new class of event-driven programming patterns.
The online landscape has changed rapidly in the past few years and doesnt show any signs of slowing down. The explosion of the social web has meant one big thing for us: more people are online now than ever before, and the demographic has forever shifted away from technical users. The Internet is for all of us, and the winners in this new space will be those companies that can figure out how to make the online experience warm and human by truly connecting individuals to each other.
Using JavaScript to connect your systems puts you at an advantage because you can quickly move from the front of the web stack dealing with human users to the backend data storage, and all of the network plumbing in between. You will be able to think of your systems as truly modular; each piece can be plugged in and deployed wherever the resources are best suited to it. You will be able to create applications that grow and breathe with your userbase unlike ever before.
Audience and Assumptions
Readers of this book should have an understanding of how websites and web applications are put together. In an effort to stay focused on the core technology, this book brushes past why web applications are built in a certain way in favor of the how.
Some knowledge of JavaScript would come in handy to fully understand the examples in this book. The examples will be thoroughly explained, but prior knowledge will help readers comprehend the back history for programming decisions made during the writing process.
Many developers approach NoSQL data stores as part of a transition from relational database systems. This book makes no assumptions about the readers proficiency in database design; I will go through the details of why I chose to make various decisions throughout the database architecting phase. MongoDB is friendly to SQL concepts, which is a major motivation for choosing it as the datastore for this project.
In the final section of the book I will discuss a selection of supporting tools and technologies that step outside of the pure JavaScript environment built in the first two sections. Readers are not expected to have a deep understanding of any of those extra languages (like Scala, Java, PHP, or Bash Scripting), but because deep exploration of these concepts are outside the scope of this book, I encourage using these examples as a launching pad for further research.
Organization
This book is broadly organized into two sections, the first providing an overview of Node.js, MongoDB, and Backbone.js (the core technology discussed in this book), and the second detailing how you can go about building a website styled as a social network using these tools. If you are new to any of these I recommend starting with the section to gain a bit of background before diving into the application in the second section. If you are already familiar with JavaScript you will probably be able to skip the first section and find yourself comfortable enough to get through the examples in the second section.
Heres how the book is organized:
Part I: Introduction
This chapter introduces JavaScript and the core concepts that will be explored throughout the book.
This chapter introduces Node.js and guides you through getting started with your first standalone applications. Here you will become acquainted with the key modules you will later use to build a complete real-time application.
Next you will explore how Backbone.js is making programming in the web browser with JavaScript more like building traditional applications and less like building websites. Well look into some of the more troubling aspects of maintaining JavaScript-based projects, and introduce templating as a way to separate your visual HTML layout from your functional JavaScript application code.
I love MongoDB because it is fast and easy to set up, easy to interface with, and speaks the same language as my Node.js applications. In this chapter well look at how to do basic querying and data manipulation as well as some more complex use cases to think about as your MongoDB usage grows.
Part II: Building a Social Network
The lack of information about how to structure and put together files in your project is one of the biggest problems facing texts that explain how to build websites. In this chapter well set up the Node.js and Backbone project files that will form the website, and lay the foundation for the rest of the code that will be coming.
Before you can do anything with your application, you need a way to create accounts and sign in. This chapter explains how to get users into your database and how to secure their data once you have it.
Now that the barebone structure and login functionality have been built, this chapter will take you through setting up the web page harness that will contain all of the content presented to your users. This is where we will go into detail on using templates with Backbone.js and integrating access control with Node.js.
The contact list is the social aspect behind this website. In this chapter you will learn how to add and remove contacts from your list, denormalizing the data into MongoDB as you go. This will be a departure for anyone coming from a relational database environment; its recommended reading!
This chapter builds upon the contact list created in by adding real-time chat functionality using Socket.io. Talk to your friends and receive messages back right away without needing to reload your page.
Finally, the user interfaces built throughout the book will be revisited in this chapter and extended with Socket.io just like the chat list. This will add life to the site by giving your users up-to-the- minute information about the comings and goings of their contacts, and turn all of the shared message spaces into interactive rooms.