patternphpMinor
Module for sending messages through PubNub
Viewed 0 times
modulesendingpubnubmessagesforthrough
Problem
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
hook_admin: //not really a hook
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: 'fo
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: 'fo
Solution
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
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
Context
StackExchange Code Review Q#36111, answer score: 3
Revisions (0)
No revisions yet.