Remote Application Development with Mozilla
Pages: 1, 2

Database Support

Databases, or more specifically interaction with databases, is what makes many Web applications tick. Work is underway on a feature that adds database support to Mozilla. This is still an experimental feature, not available in nightly or milestone builds yet, but it is being actively worked on, and there is an installable component available in the bug monitoring this feature. Database connectivity is not exclusive to remote applications, but this is an arena where it can thrive.

Why is this feature important? Web applications have traditionally been implemented with three tiers in which the middle tier is an application server that handles interactions between the client browser and the database server. Native database support makes it possible for a Mozilla-based application to eliminate the middle tier and communicate directly with the database server, reducing the complexity and cost of implementing such apps.

The caveat is that functions ordinarily performed by the application server, like authentication and authorization, must then be handled by the database server. But such functions are typically built into database servers anyway, since two-tier client-server applications with a database backend were common before three-tier Web applications became popular. Mozilla can truly come into its own as a remote application framework with database access APIs that are easily available for populating UI widgets with data.

Like much of the Mozilla codebase, database support is designed to be cross-platform. The initial implementation supports only PostgreSQL databases, but adding other SQL databases (MySQL, Oracle, Informix, Sybase, etc.) is on the agenda. There is no plan to support non-SQL database systems.

To see an example of the database support (also known as mozdb) in action, first make sure you have PostgreSQL libraries installed on your system. Some Linux distributions come with the PostgreSQL libraries installed by default, and Windows users can get them by downloading libpq.dll from the bug report and installing it into their system directory or another suitable location.

Next install mozdb by loading the latest available installer attached to the bug report. Installers are the attachments with a .xpi extension; as of this writing, the latest versions are the ones attached on December 14 for Windows and Linux.

Then restart Mozilla and configure a database alias in the Database support panel of the Mozilla global preferences.

Adding an alias
Adding an alias

Configure an alias to run against your own PostgreSQL server per the directions in the bug report, or use the following settings to access a publicly available server hosted by Eric Gentilini of the PostgreSQL community.

     Name: mozdbtest
     Type: pgsql
     Port: 5432
 Database: mozdbtest

Finally, run the example application bundled with mozdb by loading it in Mozilla or running Mozilla with the -chrome command line option set to chrome://mozdb/content/test/test.xul. When prompted for a username and password, use mozdbdemo for both. You will see a page with three tabs for different kinds of examples, including the following example of a XUL menu list and tree populated with data from the database.

Database query results in XUL
Database query results in XUL

Here is a code example illustrating how to set up a database connection via the mozdb XPCOM service.

function init() {
  var service = Components.classes[";1"]
  try {
    connection = service.getConnection("urn:aliases:test");
  catch (ex) {

This code first accesses the mozIDbService service and then calls its getConnection method to initialize a connection to the database server. Once a connection is initialized, you can use it to run queries and then populate trees, menulists, and grids with their results by retrieving them as RDF data sources. Here is a code example of a database query that populates a XUL tree in this way:

var code = document.getElementById("asyncStateCode").value;
var query = "select name from states where code = '" + code + "'";
var request = connection.asyncExecuteQuery(query, null, observer);
var ds = result.QueryInterface(Components.interfaces.nsIRDFDataSource);
var tree = document.getElementById("statesTree");

For simple queries, for example those that retrieve only a single table, results are editable, and synchronization of edited data with the remote database is automatic. Queries and updates can be executed either synchronously or asynchronously. In addition to RDF, you can also retrieve data as JavaScript objects.

Exchanging Information

Many new Web technologies are emerging to support the growing area of Web services. Standards are still being developed, and technologies are jockeying for position, but SOAP and XML-RPC have already achieved some measure of success as protocols for communicating between Web services and client applications.

We won't cover those protocols in this article. The Mozilla SOAP API has already been covered in a previous article on the DevCenter and is documented online, while the XML-RPC API is documented online in several places available from its project page.

In addition to those protocols, Mozilla provides an XMLHttpRequest component for lightweight communication between client applications and remote HTTP servers. The component allows client apps to establish a connection to a server, send and receive data, construct DOM trees from XML data, and so on. It has a simple API, which is easy to use. The following code sample demonstrates using it to retrieve an XML document:

  var request = new XMLHttpRequest();"GET", "", false);

  // request.responseText now contains the document as a string
  // request.responseXML contains the document as a DOM tree

XMLHttpRequest places no restrictions on the kind of XML data you can exchange. It doesn't even have to be valid XML, although it won't be able to parse the data into a DOM tree unless it is. You can still use invalid XML, e.g. an HTML file, in its unparsed state as a text string.

Although it isn't a remote application, the "tinderstatus" Mozilla extension described in the Creating a Mozilla Extension tutorial is a good example of how to use the XMLHttpRequest component to retrieve data from a remote server. It uses the component to fetch a remote file with information about the current state of the Mozilla source code.

XMLHttpRequest is part of the XMLExtras module, a set of Mozilla features for manipulating XML as data. The XMLExtras module is built by default and included in all releases and nightly builds of Mozilla, so it's available to any remote application that wants to take advantage of it.

Final Thoughts

Mozilla provides a variety of technologies for building remote XUL applications, from support for remote XUL itself to APIs for communication with remote Web, application, and database servers. These technologies together make Mozilla a robust platform for the next generation of Web sites, applications, and services.

For more information about developing remote XUL applications, including how to generate XUL using scripting languages like PHP and Perl, see chapter 12 of the book Creating Applications With Mozilla.

In our next article, we'll delve into the details of one or more remote XUL applications and show you how they work.

Brian King is an independent consultant who works with web and open source technologies.

Myk Melez has been working with Mozilla since 1999, when he started using the browser as a DHTML application platform.

Return to the Mozilla DevCenter.