Real-time web applications with the Meteor framework

Shooting Star

© Lead Image © Sergey Nivens, 123RF.com

© Lead Image © Sergey Nivens, 123RF.com

Article from Issue 161/2014
Author(s):

The Meteor web framework lets you do all your programming in JavaScript – and it includes some useful features for rapid development.

Many web developers use multiple programming languages for a single application: code in PHP, Ruby, Python, or Java on the server and JavaScript for the browser. Thanks to the open source Meteor [1] framework, developers can now program web applications entirely in JavaScript, with just the normal dusting of HTML and CSS. Programming in one language can significantly reduce the development overhead. Meteor also offers some automation features and a variety of prebuilt packages.

In this article, I show how to implement an interactive web application with Meteor, JavaScript, HTML5, and CSS3. As an example, I'll show you how to create a simple content management system with a list of contributions, an editor, and access controls.

Installation

Meteor is released under the MIT license and was available in a prerelease version numbered 0.7.0 when this issue went to press. When version 1.0 of the framework appears in the spring of 2014, Meteor will be ready for production. Meteor itself is written entirely in JavaScript. On the server side, Meteor relies on Node.js. The Meteor Development Group developed the Meteor software as an open source project, which was sponsored by several investors from the Internet industry.

Listing 1 installs Meteor on Ubuntu 12.04 in the user's home directory; it then generates the skeleton of the example application called Starpublisher. Line 1 first uses Apt to fetch the Curl command-line tool. Line 2 uses the tool to download the current Meteor distribution and install. Line 3 creates the Starpublisher project; line 4 changes to the project directory, and line 5 starts the sample application. Detailed documentation of Meteor is available online  [2].

Listing 1

Installation

 

Development Sequences

A Meteor application runs immediately after you create the basic framework. Figure 1 shows Starpublisher in the Firefox browser. By default, Meteor applications are accessible via HTTP on port 3000. The developer installs the required Meteor packages in the project directory and programs the application logic.

Figure 1: Meteor applications can be run after creating the application.

Meteor supports the programmer in many ways. For example, after each change, the framework restarts the web server in the project directory and automatically refreshes the browser view. Figure 2 shows Meteor's output in the terminal emulator. Meteor reports reboots here and encourages the application developer to fix bugs.

Figure 2: Meteor continuously documents the status of the development work in the terminal window.

The web framework only processes program code in JavaScript. On the server, the web server supplied with the distribution, Node.js, is responsible for this processing [3], whereas the browser uses its own JavaScript engine. Meteor picks the JavaScript code from the project directory itself.

Figure 3 shows the assignment of JavaScript files for server-side (s) or client-side (c) execution in the directory tree of a project directory. The numbers indicate the order for automatic loading. JavaScript files from the directory server are only run on the web server, whereas those in client are only run in the browser. All others run in/on both, except for the public folder (for images and static files) and the private folder (files that the server, but not the browser, is permitted to read).

Figure 3: Meteor automatically loads JavaScript code on the server (s) or browser side (c).

Additionally, the two Boolean properties (Meteor.isServer and Meteor.isClient) in Listing 2 let developers restrict the execution of JavaScript code to the web server or the browser. In the example, taken from main.js in the project directory in Figure 3, line 1 is executed by the web server and the browser, line 3 only by the web server, and line 5 only by the JavaScript engine in the browser.

Listing 2

Context-Specific Execution

 

Data Management

Meteor stores the application data in the form of JavaScript objects; the free NoSQL database system MongoDB ensures persistence. The database server is included with the Meteor distribution and does not need to be installed separately. In the browser, Meteor has access to a constantly updated copy of the data held by MongoDB. Listing 3 shows the process of storing a JavaScript object in the Articles data collection below Meteor. Line 1 opens the Articles data collection, thus instantiating a Collection type object. Line 2 saves the empty object, {}, in Articles by calling the insert() method.

Listing 3

Access via the Collection API

 

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Visual Aid

    Wekan lets self-organzing teams manage a project's workflow by tracking task ownership and progress visually.

  • Google Web Toolkit

    The Ingenious Google Web Toolkit builds optimized JavaScript applications in a hurry.

  • Helma

    The powerful Helma application server brings new powers to the JavaScript language. We'll show you how to use Helma to build a simple RSS reader.

  • Perl: AJAX

    AJAX technology adds dynamic elements to enhance sluggish websites. All it takes is a server-side Perl program and some client-side JavaScript code.

  • Linux Kernel 6.3 Release Includes Interesting Features

    Although it's not a Long Term Release candidate, Linux 6.3 includes features that will benefit end users.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News