Introduction
indexedDB
HTML5 provides serveral storage mechanisms to let web applications store their data for various purposes, indexedDB is one of the mechanism, it’s a transactional database system with the ability of storing JavaScript objects. Another important character it has is to be usedin conjunction with Web Workers.
By using indexedDB, powerful web applications with query and offline storage abilities can be built.
Web Workers
Web Workers mechanism provides a way for building concurrent web applications, it allows laborious tasks to be performed in a background thread, so the UI can be more responsive to their users.
The way of Inter-thread communication of Web Workers is much like Erlang design principles: message passing, and every thread is in its independent scope, so they are easy to use because no global variables, data sharing and locks.
Basic pattern
Spawning a new Web Workers to represent a server
1 | //Save the return value for later use. |
Listening messages sent from client or UI thread
1 | //Note: the self can be omitted. |
Opening the indexedDB on demand
1 | function openDataBase(callback) { |
Adding, retrieving, and removing data
1 | function addRecord(record, callback) { |
Conclusion
IndexedDB and Web Workers are new ways to build powerful and high performance web applications. They are offering opportunities for introducing traditional native applications to the web platform, in other words, applications using threads and database systems running in windows or *nix systems can now be migrated into browsers!
References
A simple web app demonstrates how to use indexedDB in Web Workers