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
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
Hostname: egentili.net1.nerim.net
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
Here is a code example illustrating how to set up a database connection
via the mozdb XPCOM service.
function init() {
var service = Components.classes["@mozilla.org/mozdb/service;1"]
.getService(Components.interfaces.mozIDbService);
try {
connection = service.getConnection("urn:aliases:test");
}
catch (ex) {
alert(service.errorMessage);
}
}
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");
tree.database.AddDataSource(ds);
tree.builder.rebuild();
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();
request.open("GET", "http://www.example.com/document.xml", false);
request.send(null);
// 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.
Showing messages 1 through 8 of 8.
-
timestamp
2003-05-19 12:02:36 anonymous2 [View]
when was this written. I hate when articles don't clearly display the date they were written! -
timestamp
2003-05-19 12:21:00 mykmelez [View]
The article was published on 2002 December 17 and was written in the month or so before that. Note that you can always find out when articles were published on the O'Reilly Network by looking at their URLs, since the date an article is published is part of its URL.
-
Update on Native Database Support
2003-02-14 13:43:40 Brian King |
[View]
Note that there have been substantial changes to the Native Database Support feature discussed in this article.
The latest technical details can be found in the bug:
http://bugzilla.mozilla.org/show_bug.cgi?id=81653
and in a recent talk that Jan Varga gave:
http://eu.mozdev.org/Brussels2003/talks/jan/index.html
-
Remotely serving XUL and RDF
2003-01-28 19:47:45 anonymous2 [View]
I would like to point out that if you want your rdf datasource to work, you should add it to the application/vnd.mozilla.xul+xml mimetype. From reading this article I thought it should be added as application/xml which doesn't work.
Stephen Goddard
Sr. Web Developer
CNN.com -
Remotely serving XUL and RDF
2003-05-19 12:23:26 mykmelez [View]
Sorry I didn't see this earlier; O'Reilly's feature to notify me when a new message is posted didn't seem to work in this case.
RDF files should indeed be either "application/xml" or "text/xml". "application/vnd.mozilla.xul+xml" is the MIME type for XUL files. Perhaps you are mixing up the two?
-
Edit of most recent post.
2003-01-10 04:24:14 anonymous2 [View]
I realized that I had made a contextual mistake when writing my last comment.
I am not the developer/founder of Twisted, it is the brain-child of many other developers.
Thanks again. I noticed this error as soon as I clicked submit. Sorry for the confusion. ;)
Regards,
MJ Stahl
-
Twisted + Mozilla for Remote Applications
2003-01-10 04:20:00 anonymous2 [View]
Thanks for the great introduction article. I just wanted to kind of add a shameless plug about a currently infantile project that I have started working on.
Twisted is a python framework for distributed applications. Information about it can be found at
http://www.twistedmatrix.com/
It implements its own open protocol for communication called PB or Perspective Broker. Please check it out. It offers to much to even summarize in this forum.
For use with Twisted I started a mozdev.org project for JSPB or JavaScript Perspective Broker with the intention of giving Mozilla the ability to understand the PB protocol and in turn dynamically create an interface for an application written for Twisted.
Best regards,
MJ Stahl
-
Cool
2002-12-18 06:36:39 anonymous2 [View]
This Mozilla project is really starting to crackle. I love the database tool.









