Posted by adammalone on May 2, 2012 at 6:49am
This might seem like a simple issue however I'm trying to get a module to change the content of a block whenever it receives a listening event from the server. I've got my listener set up and that works fine as I've called the nodejs function which pushes it to users and that works. However I'm a bit at a loss as to how I can replace block content.
in short
Event listener works
want to replace block content upon event
Comments
Replacing block content
That's where jquery is your friend. Depending upon how much data you're receiving (are you getting complete html or just data elements being replaced?) you can use jquery to alter the document.
For example, I have a process that sends a JSON message out to the client browser containing the DOM ID of the element to be updated, and its new value. So in my javascript on the client, I have code that looks like this:
function ui(message) {
// For every key in the dictionary, change the innerHTML of that entry.
for (DOMid in message.data) {
$(DOMid).html(message.data[DOMid]);
}
};
(This function gets called by the callback function on the 'message' event.)
Obviously, I've crafted the message payload on the server side with the specific knowledge of which DOM elements are to be updated with the data. (In my application, the server has that information available, because I've crafted the appropriate DIV in drupal to use specific IDs.)
Hope this helps.
nodejs_ajax/nodejs_wathcdog (and other approaches)
Hi,
there are some useful pointers for a slightly more generic solution in the submodules nodejs_ajax and nodejs_watchdog included in the nodejs.module.
nodejs_watchdog.module's usage of the built-in ajax is a similar use-case. I've adapted the relevant snippets slightly below:
PHP (modelled on nodejs_watchdog.module cf. line 40+):
$commands[] = ajax_command_replace('#ID-YOU-WANT-TO-REPLACE', 'HTML TO REPLACE IT WITH');$message = (object) array(
'channel' => 'YOURMODULESCHANNEL',
'commands' => $commands,
'callback' => 'YOURMODULESCALLBACK',
);
nodejs_send_content_channel_message($message);
JS (again modelled on nodejs_watchdog):
(function ($) {Drupal.Nodejs.callbacks.YOURMODULESCALLBACK = {
callback: function (message) {
Drupal.nodejs_ajax.runCommands(message);
}
};
}(jQuery));
hth, fredrik
Yeah I'll be replacing pretty
Yeah I'll be replacing pretty much the entirety of a block's html with refreshed content. In essence reloading the block with the latest content. nodejs_watchdog sounds like it could be the way forward and as long as I get the correct #id-of-block it will probably be ok. I'll look heavily to the nodejs watchdog module for example.
I've not had a chance to research into this so it might be a simple question, but with the channels - since the block is only on one page would it be like opening a channel for people on that page (similar I'm presuming to opening the watchdog channel to people on the watchdog page)
Thanks again for your swift help!
nodejs content channels
yes, want to be using what the nodejs.module refers to as "content channels". Unlike "user channels" you'll manually "subscribe" clients via a token (in this case probably in hook_block_view).
See e.g. nodejs_watchdog_form_dblog_filter_form_alter in nodejs_watchdog.module:
nodejs_send_content_channel_token('YOURMODULESCHANNEL');You'll have to do something like that before you send the message w/ nodejs_send_content_channel_message.
best, fredrik
Looking for documentation on nodejs_send_content_channel_token
Will nodejs_send_content_channel_token act as something like
Listen to 'YOURMODULECHANNEL' but if it does not exist create it?
I am having trouble figuring out a way to check on the existence of a channel, is this one way? Do you know of another/better one?
Thank you
This is how I defined a
This is how I defined private channels:
<?php/**
* Implements hook_nodejs_user_channels().
*/
function chat_nodejs_nodejs_user_channels($account) {
if (user_access('access nodejs chat', $account)) {
return array('chat_nodejs', 'chat_nodejs_auth');
}
return array();
}
?>
If it is a public channel then users don't really need to be joined to it as everyone is joined. Does that make sense?
Nodejs to update text in block with drupal 8
Can anyone help me update text in block with drupal 8. I can provide my code here. Basically I have written the code but my callbacks are not being called. Please anyone can provide me help on this.Thank You.
Did you add your callbacks using hook_nodejs_handlers_info???
check hook_nodejs_handlers_info in nodejs.api.php.
Add your js file in libraries/yml first and then in above hook, pass your js in following format
[mymodule.js_handler_name] as return value.
you can write your callback in js file :
Drupal.Nodejs.callbacks.js_handler_name = {
callback: function (message) {
if (message.callback == 'callback_handler') {
\ your custom js script
}
}
};
The callback_handler defined above is callback parameter passed in your message object in php end.
Link : https://www.drupal.org/node/1728396