I have created a Drupal 6 module that sends messages through PubNub when the user updates information about a Node. I have a separate app that, when launched, calls my Drupal site and receives a JSON array containing a lot of information about some nodes. I then update the info in my other app by PubNub->publish from Drupal every time the page is reloaded after the Node is updated. I have the following in my foo.module
file:
hook_admin: //not really a hook
function moduleName_admin(){
//Here we are pulling in the Pubnub.php file from the module directory
// NOTE: This file requires PubnubAES.php even when you aren't using encryption so include it too
module_load_include('php', 'moduleName', 'Pubnub');
$pubnub = new Pubnub(
"PUBKEY",
"SUBKEY",
"",
false
);
$form = array();
$query = "SELECT info FROM tables;"
$results = db_query($query);
while($r=db_fetch_object($results)){
$form['moduleName_fieldDescription_'.$r->nid] = array(
'#type' => 'radios',
'#title' => "Select one for ".$r->info.":",
'#options' => array(
t('Foo'),
t('Bar'),
t('Baz')
),
'#default_value' => variable_get('moduleName_fieldDescription_'.$r->nid, '0'),
);
$pubnub->publish(array(
'channel' => 'channelName',
'message' => array("id"=>$r->nid, "selected"=>variable_get('moduleName_fieldDescription_'.$r->nid, '0'))
));
}
return system_settings_form($form);
}
JSON page:
function moduleName_JSON(){
$query = "SELECT info FROM table;"
$results = db_query($query);
$infoObject = array();
for($i=0;$r = db_fetch_object($results); $i++){
$infoObject[$i] = array(
'info' => $r->info,
'id' => $r->nid,
'selected' => variable_get('moduleName_fieldDescription_'.$r->nid, '0'),
);
}
//This next part will wrap the JSON in a function so jQuery can be called like
//$.ajax( {
//url:"example.com/moduleName/json",
//dataType: 'jsonp',
//callback: 'foo',
//success:function(data){
//checkListInfo = data;
//styleSheetLoaded();
//}
//} );
$callback = check_plain($_REQUEST['callback']);
if (isset($callback) && $callback != '') {
header("Content-type: text/javascript");
echo $callback ."(";
drupal_json($infoObject);
echo ");";
} else {
drupal_json($infoObject);
}
}
hook_menu:
function moduleName_menu(){
$items = array();
$items['moduleName/editPage'] = array(
'title' => 'Module edit page',
'description' => 'Edit this page to make changes to pubnub',
'page callback' => 'drupal_get_form',
'page arguments' => array('moduleName_admin'),
'access arguments' => array('Edit moduleName Permissions'),
'type' => MENU_NORMAL_ITEM,
);
$items['moduleName/json'] = array(
'title' => 'JSON Array containing module info',
'page callback' => 'moduleName_JSON',
'access arguments' => array('Change moduleName values'),
'tpye' => MENU_CALLBACK,
);
return $items;
}
This is slightly changed up code from my working Drupal 6 module that uses a live PubNub account. It works great for my purpose. I just want to know how to do it a little more to Drupal standards.
I know I probably have the publish time wrong. Right now it publishes when the page is reloaded instead of right before the page sends the info. I don't think this is right but I am very new to Drupal.
There is a Drupal Module that is in the process of being built that will allow PubNubs integration in Drupal 7/8 but I am on D6 and I am just including the files myself.