Duplicate Messages
-
When I refresh or load the page that I have the chat on everything shows up properly but then a few seconds later I receive the new message sound and the last message is duplicated. Any suggestions?
Chris
-
Hmm.. not sure but I recall a similar situation where jQuery was included twice on the same page..?
I just had the same effect: “new message sound and the last message is duplicated”.
I investigated a bit further and found it is related to the $chat_order setting. If you set chat order to have newest messages at the bottom, then this probably happens each time, since the coding extracts the $lastID sent from the top line of the messages (which is not the newest one for this sort order). Consequently, it draws the next message from the database, repeating the last one.Well, to correct this problem, some changes are needed in simple-ajax-chat-form.php. Unfortunately it’s not only required to fix this, but due to another reason it’s also necessary to fix the select order from the DB to be DESC always (independent from $chat_order). Otherwise messages might get lost. For that reason, code changes are a bit larger then only a few lines. The redesign in this area also enables the UI to adjust to the different $chat_order as well (having time since last message at the bottom, too).
It starts near line 40 (see the areas marked as #MODIFIED#):
$current_user = wp_get_current_user(); $logged_username = sanitize_text_field($current_user->display_name); // $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM ". $table_prefix ."ajax_chat ORDER BY id ". $display_order ." LIMIT %d", $sac_number_of_comments)); // #MODIFIED# (deleted) // #MODIFIED# The order of selected IDs should always be descending (independent of display order), otherwise new messages over limit are ignored. $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM ". $table_prefix ."ajax_chat ORDER BY id DESC LIMIT %d", $sac_number_of_comments)); // #MODIFIED# (added) echo '<div id="simple-ajax-chat">'; echo $custom_chat_pre; echo '<div id="sac-content"></div>'; echo '<div id="sac-output">'; $sac_first_time = true; // #MODIFIED# (start) : Use temporary variable $sac_output and $sac_lastout to store output instead of direct echo (to enable $chat_order) $sac_output = ""; $sac_lastout = ""; if ($results) { foreach($results as $r) { $chat_text = sanitize_text_field($r->text); $chat_time = sanitize_text_field($r->time); $chat_id = sanitize_text_field($r->id); $chat_url = sanitize_text_field($r->url); $chat_name = sanitize_text_field($r->name); $name_class = preg_replace("/[\s]+/", "-", $chat_name); $chat_text = preg_replace("<code>(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?</code>i", "<a href=\"\\" target=\"_blank\" title=\"Open link in new tab\">\\</a>", $chat_text); if ($sac_first_time == true) $lastID = $chat_id; if ($use_url) $url = (empty($chat_url) && $chat_url = "http://") ? $chat_name : '<a href="'. $chat_url .'" target="_blank">'. $chat_name .'</a>'; else $url = $chat_name; if ($sac_first_time == true) { $sac_lastout = '<div id="sac-latest-message">'. '<span>'. __('Latest Message:', 'sac') .'</span> ' . '<em id="responseTime">'. sac_time_since($chat_time) .' '. __('ago', 'sac') .'</em>' . "</div>\n"; } $sac_out = '<li class="sac-chat-message sac-static sac-user-' .$name_class. '">'."\n" . ' <span title="Posted ' .sac_time_since($chat_time) .' '. __('ago', 'sac'). '">' . $url .' : </span> ' .convert_smilies(' '. $chat_text) ."\n" . "</li>\n"; if ($chat_order) { $sac_output = $sac_out . $sac_output; } // put next (older) message in front, have latest message at bottom else { $sac_output .= $sac_out; } // put next (older) message behind, have latest message at top $sac_first_time = false; } } else { $sac_output += '<li>You need <strong>at least one entry</strong> in the chat forum!</li>'; } $sac_output = '<ul id="sac-messages">' . "\n" . $sac_output . "</ul>\n"; if ($chat_order) echo $sac_output . $sac_lastout; else echo $sac_lastout . $sac_output; // #MODIFIED# (end) echo '</div>'; echo $custom_chat_app; echo $custom_form_pre; ?>Finally, to have the chat window scroll to the bottom also when the page loads (not only when a new message appears), another small change is required to let this happen.
This time it’s in sac.php, where function initJavascript() needs to be extended at the end. I recommend to includesome linebreaks to this function, as it is hard to debug and overlook otherwise.document.getElementById("sac-output").onmouseover = function(){ if(sac_loadtimes>9){ sac_loadtimes=1; receiveChatText(); } sac_timeout=sac_org_timeout; } <?php // #MODIFIED# (start) $chat_order = $sac_options['sac_chat_order']; if ($chat_order) : ?> jQuery('#sac-output').animate({ scrollTop: jQuery('#sac-output').prop('scrollHeight') }, 300); <?php endif; // #MODIFIED# (end) ?> }It would be nice to find these changes in a next version of “Simple Ajax Chat”. I really like this plugin for it’s simplicity and independence from other requirements. Thank you!
Wow thanks for taking the time, will definitely have a closer look at this for the next update and try implement if all looks good. Cheers.
MartinW2, thank you for the work you put into this – it all looks great and now is implemented in SAC v20150316 🙂 Thank you!
The topic ‘Duplicate Messages’ is closed to new replies.