Fast Drupal Ajax calls for High Traffic sites

Do you have a High traffic Drupal site that has a lot of Ajax callbacks? Use js.module to just execute what is needed for your call & cut off Drupal bootstrap!

To enable your module to use js.module, you need to follow the below steps:

  1. Your server must support URL rewrites.
  2. Clean URLs need to be activated in your Drupal setup.
  3. Its hook_js() implementation look like this:
    function example_js() { return array( 'path' => array( 'callback' => 'example_somefunction', 'includes' => array('theme', 'unicode'), 'dependencies' => array('locale', 'filter', 'user'), 'bootstrap' => DRUPAL_BOOTSTRAP_CONSTANT, 'file' => 'includes/example.inc', 'access arguments' => array('e.g. permission'), 'access callback' => 'callback function', 'page arguments' => array() ), ); }
  4. As in the js module, use hook_js instead of hook_menu and Ajax event URL should be like <js/module_name/function_name>. include both hook_menu & hook_js so the module works with or without js module. if js module is enabled the hook_js will be used & hook_menu otherwise.
  5. Please note  & include any specific files that need to be executed to make your call back function. (REMEMBER this will not have access to files that will normally be available as it skips bootstrap). Note if we have too many includes it will defeat the pourpose of using this module as well.
  6. eg. admin menu module includes Another trick that the admin module uses to simplyfy the code is to define the path as a variable & call it throughout.

Remember to follow instructions in the js.module README file, https://drupal.org/project/js

Attached an example file that demonstrates the same. In the example module the ajax call is using drupal menu callback triggered using event in jquery.

Some good working examples of Drupal 7 modules that support js.module are Administration menu, Chatblock and Collapser.

XHProf is an hierarchical profiler. It gives the report to check that how much time takes to load the function call and there is an option to view the report in User Interface itself. Below is a XHprof report of the ajax call with and without js module

Fast Drupal Ajax calls for High Traffic sites

Note that the function calls reduce by half, total Incl. Wall Time, total Incl. MemUse are also reduced while using js.module. This means a lot of performance benefit to a High traffic Drupal site that run multiple Ajax callbacks.

Attachment: example.tar.gz