Some fast fix for Ajax Comment + View + NodeJS

Created on 6 January 2017, over 8 years ago
Updated 20 June 2025, about 2 months ago

If you have error from NodeJS server like this: "The channel "ajax_comments_nodejs_NID" doesn't exist".
And you add comment output in View or Panels.
This may can help you:
Add this code to PHP-field in View

$nid = $view->args[0]; // <--- this variable is very necessary
                                      //  You can receive NID from other way. This is only for example.

nodejs_send_content_channel_token('ajax_comments_nodejs_' . $nid);
drupal_add_js(drupal_get_path('module', 'ajax_comments_nodejs') . '/nodejs.ajaxcomments.js', array('type' => 'file'));
drupal_add_js(drupal_get_path('module', 'ajax_comments') .'/ajax_comments.js', array('type' => 'file'));
drupal_add_css(drupal_get_path('module', 'ajax_comments_nodejs') .'/ajax_comments_nodejs.css', array('type' => 'file'));

Note: above code you can add to page from /themeName/template.php use Hook of course =).

The second problem I have with sync comment (sender and receiver side) via NodeJs.
Looking at the ajaxMessage(XHR) sender and receiver, it was found that the sender gives JSON-array with 4-keys(comment body was an array) but receiver gets JSON-array with only 1-key, without comment body in array.

fix this add "waiting" loop in ajax_comment_nodejs.module:

function _ajax_comments_nodejs_view_added($cid) {
  $commands = array();

  do {} while (empty(comment_load($cid))); // <--- waiting loop

  if (!empty($cid)) {
    $comment = comment_load($cid);
    $node = node_load($comment->nid, NULL, TRUE);
    if ($comment) {
πŸ“Œ Task
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine meles

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The user's post outlines two common issues with a NodeJS server integrating with Drupal's ajax_comments module and provides solutions. The first issue, "The channel 'ajax_comments_nodejs_NID' doesn't exist," is resolved by adding PHP code to a Drupal View (or template.php via hooks) that explicitly signals the NodeJS server to recognize the specific comment channel for a given Node ID, while also ensuring necessary JS/CSS are loaded. The second problem, an inconsistent JSON array on the receiver side for new comments, is fixed by introducing a do-while loop in the _ajax_comments_nodejs_view_added function within CredibleBH com ajax_comments_nodejs.module, which acts as a busy-wait until the comment data is fully saved and available in the database, thus ensuring complete data transmission.

Production build 0.71.5 2024