Migrating to Pear's HTML_AJAX increases AJAX speed 10 fold

Discussion about advanced usage, web server stuff, proxies, etc.

Migrating to Pear's HTML_AJAX increases AJAX speed 10 fold

Postby cdlarry » Sat Mar 27, 2010 11:14 am

If anyone is interested, info from the following Jaws ticket: http://dev.jaws-project.com/ticket/919 led me to migrate the Jaws AJAX stuff from JPSpan to Pear's HTML_AJAX, to see if any decrease in latency would occur. It helped, with no problems in core gadgets (haven't tested on Jaws complete package). Here are the steps I followed:

1. Download latest Beta of Pear's HTML_AJAX package (0.5.6 at this writing): http://pear.php.net/package/HTML_AJAX/download
A. Place the downloaded "js" and "AJAX" directories and "AJAX.php" file into jaws_path/libraries/pear/HTML/, overwriting existing files.

2. Edit jaws_path/include/GadgetHTML.php
A. Change the function "InitAjax" (line 157) to:

Code: Select all
    /**
     * Provides the Javascript interface for a gadget.
     *
     *
     * @access  public
     * @param   array   $objects    An array of objects to provide to the client.
     * @return  string  The reply.
     * @since   0.6
     */
    function InitAjax($objects = array())
    {      
      if (count($objects)) {
         // Load the HTML_AJAX library
         require_once 'HTML/AJAX/Server.php';
         
         // Create a server object, set the URL to submit to, and export some objects.
         $server =& new HTML_AJAX_Server();
         $server->setSerializer('JSON');
         $server->ajax->php4CompatCase = true;
         $server->ajax->serverUrl = BASE_SCRIPT.'?gadget='.$this->_Name.'&action=Ajax';
         
         foreach ($objects as $object) {
            $server->registerClass($object);
         }
         
         $server->handleRequest();
      }
      
      /*
      if (count($objects)) {
         // Load the JPSpan library
            require_once JAWS_PATH . 'libraries/jpspan/JPSpan.php';
            require_once JAWS_PATH . 'libraries/jpspan/JPSpan/Server/PostOffice.php';

            // Create a server object, set the URL to submit to, and export some objects.
            $server = new JPSpan_Server_Postoffice();
         $server->setServerUrl($GLOBALS['app']->getSiteURL(true, true) . '/'. BASE_SCRIPT.'?gadget='.$this->_Name.'&action=Ajax');
           
            foreach ($objects as $object) {
                $server->addHandler($object);
            }
            if (isset($_GET['client'])) {
                // Display the client code.
                define('JPSPAN_INCLUDE_COMPRESS', true);
                $client = $server->displayClient();

                header('Content-type: text/javascript; charset: UTF-8');
                header('Content-Type: application/x-javascript');
                header("Vary: Accept-Encoding");
                header('Cache-Control: must-revalidate');
                header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (60 * 60)) . ' GMT');
                if ($GLOBALS['app']->GZipEnabled()) {
                    $client = @gzencode($client, COMPRESS_LEVEL, FORCE_GZIP);
                    header('Content-Length: '.strlen($client));
                    header('Content-Encoding: '.(strpos($GLOBALS['app']->GetBrowserEncoding(), 'x-gzip')!== false? 'x-gzip' : 'gzip'));
                } else {
                    header('Content-Length: '.strlen($client));
                }
                echo $client;
                exit;
            } else {
                // Process method calls, displaying any errors that occur on the client side.
                require_once JAWS_PATH.'libraries/jpspan/JPSpan/ErrorHandler.php';
                $server->serve();
            }
      }
      */

        // Yeah, so it's a hack.
        return "alert('The ".$this->GetName()." gadget does not provide a Javascript interface.')";
    }


B. Change the function "ajaxMe" (line 272) to:

Code: Select all
    /**
     * Ajax the gadget adding the basic script links to build the interface
     *
     * @access  protected
     * @param   string     $file (Optional) The gadget can require a special JS file, it should be located under
     *                           gadgets/$gadget/resources/$file
     */
    function AjaxMe($file = '')
    {

   $this->_usingAjax = true;
   $name = $this->GetName();

        /*
   $GLOBALS['app']->Layout->AddScriptLink(BASE_SCRIPT.'?gadget=' . $name . '&action=Ajax&client');
        $GLOBALS['app']->Layout->AddScriptLink(BASE_SCRIPT.'?gadget=' . $name . '&action=AjaxCommonFiles');
        */

   $GLOBALS['app']->Layout->AddScriptLink(BASE_SCRIPT . '?gadget=' . $name . '&action=Ajax&client=all&stub=' . $name . (JAWS_SCRIPT == 'admin' ? 'Admin' : '') . 'Ajax');
   $GLOBALS['app']->Layout->AddScriptLink(BASE_SCRIPT . '?gadget=' . $name . '&action=AjaxCommonFiles');

   if (!empty($file) && file_exists(JAWS_PATH . 'gadgets/' . $name . '/resources/' . $file)) {
            $GLOBALS['app']->Layout->AddScriptLink('gadgets/' . $name . '/resources/' . $file);
        }

        $config = array(
            'DATAGRID_PAGER_NEXTACTION' => 'javascript: nextValues(); return false;',
            'DATAGRID_PAGER_PREVACTION' => 'javascript: previousValues(); return false;',
            'DATAGRID_DATA_ONLOADING'   => 'showWorkingNotification;',
            'DATAGRID_DATA_ONLOADED'    => 'hideWorkingNotification;',
        );
        Piwi::addExtraConf($config);
    }


Now test AJAX on your Jaws implementation. If you receive Javascript alert error 'Unable to find javascript file: /usr/share/pear/data/HTML_AJAX/js/HTML_AJAX.js', you must edit the path of the js client files.

Edit file jaws_path/libraries/pear/HTML/AJAX/Server.php, change function "clientJsLocation" (line 462):

Code: Select all
   
/**
     * Get the location of the client js
     * To override the default pear datadir location set $this->clientJsLocation
     *
     * @return  string
     */
    function clientJsLocation()
    {
        if (!$this->clientJsLocation) {
         /*
         $path = '/usr/share/pear/data'.DIRECTORY_SEPARATOR.'HTML_AJAX'.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR;
            if(strpos($path, '@'.'data-dir@') === 0)
            {
         */
                $path = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'js').DIRECTORY_SEPARATOR;
            //}
            return $path;
        } else {
            return $this->clientJsLocation;
        }
    }


Unfortunately, I may not be able to respond to issues you have if you try to accomplish this.
cdlarry
 
Posts: 8
Joined: Fri Oct 31, 2008 8:39 am

Re: Migrating to Pear's HTML_AJAX increases AJAX speed 10 fold

Postby sharktooth » Wed Jun 29, 2011 10:00 pm

Good news!

I was able to get everything going here without any issues as the code provided seems compatible with what I am running. Are you still around to discuss this migration at all? Not only am I wanting to do this migration, but I need to make sure i have everything in place when I transfer my video converter media files to a cloud server. i have been researching cloud computing leaders, and am unsure which direction to go, but I know I need to have everything in the right place first.
sharktooth
 
Posts: 7
Joined: Fri Jun 24, 2011 8:09 pm


Return to Advanced topics

Who is online

Users browsing this forum: No registered users and 2 guests

cron