4
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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

\$\endgroup\$
6
  • \$\begingroup\$ I thought about this but I don't really have a reason to publish the message before the form is submitted and it is easier to do the PHP pubnub because it is server-side and doesn't rely on the client-side javascript. Also it would be kinda tricky to implement because of the way Drupal forms are generated. Thanks for the suggestion. \$\endgroup\$
    – DutGRIFF
    Commented Nov 26, 2013 at 19:42
  • \$\begingroup\$ Surely! And if you have any additional PubNub-specific questions, feel free to ping us at [email protected], we're here to help. \$\endgroup\$
    – Geremy
    Commented Nov 27, 2013 at 0:22
  • \$\begingroup\$ Could you give some code examples to show the OP how to do something? That would garner an up-vote from me. \$\endgroup\$
    – syb0rg
    Commented Feb 5, 2014 at 4:55
  • \$\begingroup\$ @syb0rg please clarify what you are looking for? \$\endgroup\$
    – Geremy
    Commented Feb 7, 2014 at 2:54
  • \$\begingroup\$ I'd need a more specific example of exactly what you'd be looking for an example of... need a use case, etc @syb0rg \$\endgroup\$
    – Geremy
    Commented Feb 7, 2014 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.