Module for sending messages through PubNub 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-&gt;nid] = array(
    '#type' =&gt; 'radios',
    '#title' =&gt; "Select one for ".$r-&gt;info.":",
    '#options' =&gt; array(
      t('Foo'),
      t('Bar'),
      t('Baz')
    ),
    '#default_value' =&gt; variable_get('moduleName_fieldDescription_'.$r-&gt;nid, '0'),
  );

    $pubnub-&gt;publish(array(
      'channel' =&gt; 'channelName',
      'message' =&gt; array("id"=&gt;$r-&gt;nid, "selected"=&gt;variable_get('moduleName_fieldDescription_'.$r-&gt;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' =&gt; $r-&gt;info,
      'id' =&gt; $r-&gt;nid,
      'selected' =&gt; variable_get('moduleName_fieldDescription_'.$r-&gt;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) &amp;&amp; $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' =&gt; 'Module edit page',
    'description' =&gt; 'Edit this page to make changes to pubnub',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('moduleName_admin'),
    'access arguments' =&gt; array('Edit moduleName Permissions'),
    'type' =&gt; MENU_NORMAL_ITEM,
  );
  $items['moduleName/json'] = array(
    'title' =&gt; 'JSON Array containing module info',
    'page callback' =&gt; 'moduleName_JSON',
    'access arguments' =&gt; array('Change moduleName values'),
    'tpye' =&gt; 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.
If you are receiving the data you expect, I think you are 99% there. 

Alternatively, you could include the PubNub JS client into your Drupal app (vs. the PHP client), then you can publish asynchronously, or even completely after page load.

Link to the JS client and docs are available here, with a specific example of how to publish here