Nitobi
About Nitobi
Services
Products
Home -> Blogs -> Dave Johnson

Dave Johnson

AJAX Offline

October 8th, 2005

Just a quick post about running AJaX offline. With the recent release of Flash 8 it seems a good time to bring this up since Flash 8 has everything you need for a robust cross-browser data persistence layer. The two main features of Flash 8 are the SharedObject (although this has been around for a few years now - since Flash 6) and more importantly full JavaScript support. The SharedObject allows you to store as much data as the end user wants to allow and this limit can be configured by the end user.

In the Flash file you can use the ExternalInterface to expose methods to the JavaScript runtime like this:

import flash.external.ExternalInterface;
ExternalInterface.addCallback("JavaScriptInterfaceName", null, FlashMethod);

Similarly you can call JavaScript functions from within the Flash file using the call method of the ExternalInterface

ExternalInterface.call("JavaScriptMethod");

And you can call methods in the Flash movie from JavaScript like this:

function CallMethod() {
if (document.getElementById)
myFlash = document.getElementById("FlashMovieNodeId");
if (myFlash)
myFlash.flashFunction();
}

The other piece of the puzzle is the SharedObject which is accessible through ActionScript in Flash.

var my_SO:SharedObject = SharedObject.getLocal("MySharedObjectName");
my_SO.data.eba = dat;
var ret_val = my_SO.flush();

It is importatnt to note that if the end user specified data size limit is smaller than the requested amount of data to be saved it needs to show the Flash movie to the user (do this simply with a little JavaScript) so the user can either increase the cache size or not. The ret_val from the SharedObject flush method can be checked to see the result and determine if the Flash movie needs to be shown for input or not.

The other great thing about this is that the upgrade path to Flash 8 has been made a bit better. You can actually have the Flash 8 player installed through the old Flash player from what I understand (called Express Install I believe).

All and all a pretty slick way to persist data with AJaX without relying on cookies or the User Data behaviour in IE.

Del.icio.us

This entry was posted on Saturday, October 8th, 2005 at 12:09 pm and is filed under AJAX, Flash, Flex, JavaScript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

5 Responses to “AJAX Offline”

  1. Brad Neuberg Says:

    Hi Dave. I did exactly this, but made it compatible with Flash 6+ forward. It�s still in beta, but it�s open source. See http://codinginparadise.org/weblog/2005/10/permanent-client-side-storage-for-ajax.html for details.

  2. Jep Says:

    Dave,

    Do you have ideas how can users retrieve the stored information when they�re offline? Say, you have an RSS Reader, and the downloaded messages are saved locally when you�re online. You close the web browser and disconnect from the network: now what do you do to read the RSS messages? I guess you open the web browser. But which URL do you type? Do you open a web page from the file system? Do you need a local web server? What are your thoughts?

    I would first think of opening a file from the file system, or actually, you should have the entire application locally (which could be 1 or more files). This would be fairly easy to install: download a zip file, expand it, and you�re done. Or maybe it�s possible to store the application�s files in the Flash storage area, do you know?

    Just some random thoughts�

    Thanks,
    Jep

  3. Dave Johnson Says:

    The project looks pretty nice Brad - having made a proof of concept I am happy that someone else has created a project from it ;)

    Jep I am not sure about this � certainly it would store data between online sessions and work if you leave the browser open from an online to offline session. Haven�t really thought about how it would work out in the cases you have outlined.

    dave

  4. Julien Couvreur Says:

    FYI, an online/offline wiki prototype: http://blog.monstuff.com/archives/000272.html

    The Flash object uses the same technique that you mention. One concern tough: ExternalInterface can get quite slow with large data (100K). That�s not a big problem for a wiki, but it was slowing down my other prototype app (not published yet): an online/offline RSS reader.

  5. Brad Neuberg Says:

    Dave, ExternalInterface has significant performance problems; this is why I use the Flash 6+ Flash methods to do Flash/JavaScript communication, such as GetVariable, SetVariable, fscommand, etc. These can reliably pass around megabytes of information very quickly. I use ExternalInterface only for Safari support. I�m rolling all of this into dojo.flash (which is used by dojo.storage), so that others can use it in their own projects. It was _really_ hard to find the exact performant, compatible, non-buggy path to achieve the stuff above, but I think I finally found it so that its easier for others to use in their own projects.

Leave a Reply


Search Posts

Pages

Archives

Categories

All contents are (c) Copyright 2006, Nitobi Software Inc. All rights Reserved
Dave Johnson Entries (RSS) and Comments (RSS).