This project is not covered by Drupal’s security advisory policy.
PubSub provides an API for publishing messages and subscribing to published messages. Additionally, it provides a cascading structure for messages allowing modules to extend PubSub in creative ways.
You publish a message by calling pubsub_publish('your.message', $any, $args, $you, $want)
. Messages are expanded and sent to all subscribers. For example:
Calling the following method:
pubsub_publish('foo.bar.baz', $arg1, $arg2);
Will execute the following hooks:
hook_pubsub('foo.bar.baz', $arg1, $arg2);
hook_pubsub_foo('bar.baz', $arg1, $arg2);
hook_pubsub_foo_bar('baz', $arg1, $arg2);
Examples
Publishing and subscribing to messages.
/**
* Implementation of hook_nodeapi().
*/
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'update':
pubsub_publish('node.update', $node);
}
}
/**
* Implementation of hook_pubsub().
*/
function mymodule_pubsub($message, $node) {
switch ($message) {
case 'node.update':
/* Do something awesome with $node */
break;
}
}
/**
* Implementation of hook_pubsub_MESSAGE().
*/
function mymodule_pubsub_node($message, $node) {
switch ($message) {
case 'update':
/* Do something awesome with $node */
break;
}
}
Sub Modules
Out of the box, PubSub comes with 2 sub modules: PubSub Cron and PubSub JavaScript.
PubSub Cron
PubSub Cron re-publishes all messages of the pattern cron.* when Drupal's cron runs. This can be very useful as a sort of message queue, allowing you to offload expensive operations to cron.
Example
/**
* Implementation of hook_init().
*/
function mymodule_init() {
// Publish a message to PubSub Cron.
pubsub_publish('cron.mymodule.init', 1, 2, 3, 4);
}
/**
* Implementation of hook_pubsub_MESSAGE().
*
* This hook will be called when Drupal's cron runs.
*/
function mymodule_pubsub_mymodule($message, $arg1, $arg2, $arg3, $arg4) {
switch ($message) {
case 'init':
/* Do something awesome */
break;
}
}
PubSub JavaScript
PubSub JavaScript re-publishes all messages of the pattern js.* to JavaScript, executing subscribers in Drupal.behaviors.
Example
/**
* Implementation of hook_init().
*/
function mymodule_init() {
// Publish a message to PubSub JavaScript
pubsub_publish('js.mymodule.init', 1, 2, 3, 4);
}
In JavaScript:
/**
* Subscribe to the js.mymodule.init message.
*/
Drupal.pubsub.subscribe('mymodule.init', function(one, two, three, four) {
/* Do something awesome */
});
Project information
- Module categories: Developer Tools
- Created by pifantastic on , updated
- This project is not covered by the security advisory policy.
Use at your own risk! It may have publicly disclosed vulnerabilities.