You are here

README.txt in JS Callback Handler 5.2

Same filename and directory in other branches
  1. 6 README.txt
  2. 7 README.txt
-- SUMMARY --

JavaScript callback handler is an interim solution for high-performance querying
of contents including (but not limited to) AHAH, AJAX, JSON, XML, etc.

Apache benchmarks speak for itself:

Using index.php as usual:
  ab -n20 -c1 http://example.com/index.php?q=js/mymodule/callback
  Requests per second: 2.24 [#/sec] (mean)
  Time per request:    446.846 [ms] (mean)

Using js.php:
  ab -n20 -c1 http://example.com/js.php?q=js/mymodule/callback
  Requests per second: 16.84 [#/sec] (mean)
  Time per request:    59.371 [ms] (mean)

Note that this module does nothing on itself.

For a full description visit the project page:
  http://drupal.org/project/js
Bug reports, feature suggestions and latest developments:
  http://drupal.org/project/issues/js


-- INSTALLATION --

* Install as usual, see http://drupal.org/node/70151 for further information.

* Copy js.php to the root directory of your Drupal installation.

* Enable clean URLs.

* Add the following lines in front of the existing RewriteRules in your
  .htaccess file:

    # Rewrite JavaScript callback URLs of the form 'js.php?q=x'.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^\/js\/.*
    RewriteRule ^(.*)$ js.php?q=$1 [L,QSA]


-- DEVELOPER INFORMATION --

This module requires an Apache RewriteRule in your .htaccess file to point all
paths starting with js/ to js.php rather than index.php.  The 2nd argument
following js/ determines the implementing module, which must implement hook_js()
for security reasons.  Apart from security, modules may also specify another
bootstrap level than the default DRUPAL_BOOTSTRAP_PATH, and additionally
required includes files and module dependencies to load.

As an example, we'll let example.module expose its function
example_somefunction() to js.php. Its hook_js() implementation might look like
this:

<code>
  function example_js() {
    return array(
      'somefunction' => array(
        'callback' => 'example_somefunction',
        'includes' => array('theme', 'unicode'),
        'dependencies' => array('locale', 'filter', 'user'),
      ),
    );
  }
</code>

The hook_js() implementation above makes JS accept the following URL:

  http://example.com/js/example/somefunction.
                        ^       ^
           module name -|       |
                info array key -|

When called, it loads the requested include files and modules and calls the
callback function.

Note that it is wise to also register a corresponding menu path in hook_menu()
to provide fallback functionality when js.php is not available:

<code>
  $items[] = array(
    'path' => 'js/example/somefunction',
    'callback' => 'example_somefunction',
    'type' => MENU_CALLBACK,
  );
</code>

As stated above, js.php bootstraps Drupal to DRUPAL_BOOTSTRAP_PATH and includes
common.inc and locale.inc.  This means that url(), l(), and t() functions are
available (t() lacks translation though, since that would require locale.module
to be loaded.  This can be easily "fixed" by adding locale to the dependencies
array).  Theme functions and potentially required functions of other modules,
however, are not available by default, which is the main reason for the speed
gain of js.php.  Since the session has been initialized, the global $user
object is also available (but may not be fully loaded).

Please note that js.php does NOT perform access checks like Drupal's menu
system.  If required, each callback function needs to do this on its own.

js.php outputs the return value of the callback function using drupal_to_js().
To use a custom output format, output the data on your own and exit()
afterwards, or simply return nothing.


-- CREDITS --

Authors:
* Daniel F. Kudwien (sun) - http://drupal.org/user/54136
* Stefan M. Kudwien (smk-ka) - http://drupal.org/user/48898

This project has been sponsored by UNLEASHED MIND.
Specialized in consulting and planning of Drupal powered sites, UNLEASHED
MIND offers installation, development, theming, customization, and hosting
to get you started. Visit http://www.unleashedmind.com for more information.

File

README.txt
View source
  1. -- SUMMARY --
  2. JavaScript callback handler is an interim solution for high-performance querying
  3. of contents including (but not limited to) AHAH, AJAX, JSON, XML, etc.
  4. Apache benchmarks speak for itself:
  5. Using index.php as usual:
  6. ab -n20 -c1 http://example.com/index.php?q=js/mymodule/callback
  7. Requests per second: 2.24 [#/sec] (mean)
  8. Time per request: 446.846 [ms] (mean)
  9. Using js.php:
  10. ab -n20 -c1 http://example.com/js.php?q=js/mymodule/callback
  11. Requests per second: 16.84 [#/sec] (mean)
  12. Time per request: 59.371 [ms] (mean)
  13. Note that this module does nothing on itself.
  14. For a full description visit the project page:
  15. http://drupal.org/project/js
  16. Bug reports, feature suggestions and latest developments:
  17. http://drupal.org/project/issues/js
  18. -- INSTALLATION --
  19. * Install as usual, see http://drupal.org/node/70151 for further information.
  20. * Copy js.php to the root directory of your Drupal installation.
  21. * Enable clean URLs.
  22. * Add the following lines in front of the existing RewriteRules in your
  23. .htaccess file:
  24. # Rewrite JavaScript callback URLs of the form 'js.php?q=x'.
  25. RewriteCond %{REQUEST_FILENAME} !-f
  26. RewriteCond %{REQUEST_FILENAME} !-d
  27. RewriteCond %{REQUEST_URI} ^\/js\/.*
  28. RewriteRule ^(.*)$ js.php?q=$1 [L,QSA]
  29. -- DEVELOPER INFORMATION --
  30. This module requires an Apache RewriteRule in your .htaccess file to point all
  31. paths starting with js/ to js.php rather than index.php. The 2nd argument
  32. following js/ determines the implementing module, which must implement hook_js()
  33. for security reasons. Apart from security, modules may also specify another
  34. bootstrap level than the default DRUPAL_BOOTSTRAP_PATH, and additionally
  35. required includes files and module dependencies to load.
  36. As an example, we'll let example.module expose its function
  37. example_somefunction() to js.php. Its hook_js() implementation might look like
  38. this:
  39. function example_js() {
  40. return array(
  41. 'somefunction' => array(
  42. 'callback' => 'example_somefunction',
  43. 'includes' => array('theme', 'unicode'),
  44. 'dependencies' => array('locale', 'filter', 'user'),
  45. ),
  46. );
  47. }
  48. The hook_js() implementation above makes JS accept the following URL:
  49. http://example.com/js/example/somefunction.
  50. ^ ^
  51. module name -| |
  52. info array key -|
  53. When called, it loads the requested include files and modules and calls the
  54. callback function.
  55. Note that it is wise to also register a corresponding menu path in hook_menu()
  56. to provide fallback functionality when js.php is not available:
  57. $items[] = array(
  58. 'path' => 'js/example/somefunction',
  59. 'callback' => 'example_somefunction',
  60. 'type' => MENU_CALLBACK,
  61. );
  62. As stated above, js.php bootstraps Drupal to DRUPAL_BOOTSTRAP_PATH and includes
  63. common.inc and locale.inc. This means that url(), l(), and t() functions are
  64. available (t() lacks translation though, since that would require locale.module
  65. to be loaded. This can be easily "fixed" by adding locale to the dependencies
  66. array). Theme functions and potentially required functions of other modules,
  67. however, are not available by default, which is the main reason for the speed
  68. gain of js.php. Since the session has been initialized, the global $user
  69. object is also available (but may not be fully loaded).
  70. Please note that js.php does NOT perform access checks like Drupal's menu
  71. system. If required, each callback function needs to do this on its own.
  72. js.php outputs the return value of the callback function using drupal_to_js().
  73. To use a custom output format, output the data on your own and exit()
  74. afterwards, or simply return nothing.
  75. -- CREDITS --
  76. Authors:
  77. * Daniel F. Kudwien (sun) - http://drupal.org/user/54136
  78. * Stefan M. Kudwien (smk-ka) - http://drupal.org/user/48898
  79. This project has been sponsored by UNLEASHED MIND.
  80. Specialized in consulting and planning of Drupal powered sites, UNLEASHED
  81. MIND offers installation, development, theming, customization, and hosting
  82. to get you started. Visit http://www.unleashedmind.com for more information.