Real-time web applications with the Meteor framework
Shooting Star
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.
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.
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).
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
(incl. VAT)
Buy Linux Magazine
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.
News
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.