This whole project and code will be hosted to php, mysql hosting server later.
code:
$(document).ready(function(){
console.log('hello');
$('input[name="nm_submit_comment"]').on('click', function(e){
e.preventDefault();
var frm = $(this).closest("form");
var frm_id = frm.attr("id");
var frm_id_splitted = frm_id.split("_");
var frm_id_splitted_2 = frm_id_splitted[2];
var frm_serialized = frm.serialize();
$.ajax({
url: "save-comment.php",
method: "POST",
data: frm_serialized,
success: function(data) {
data = JSON.parse(data);
$('div#id_div_comment_' + frm_id_splitted_2).append('<div class="cl_div_comment_content">' + data.nm_textarea_comment_content + '</div>');
$('textarea.cl_textarea_comment_content').val("");
}
});
});
});
jQuery ajax is working fine. It saves data to database, then appends the data to div. My question is: after appending the data, will the data be available/visible to other end(computer/user)? Or after appending data do i have to add set setInterval function to load the comment div every 10 seconds. So if someone comments then after 10 seconds the comment will be visible to other users. It may have overhead to load all comments div.
We see same scenario for facebook status updates. When someone updates his status or comments then comments are visible to other all friends. We see such example in stackoverflow notifications. If someone answers or comments, we get immediate notification. How is it done? How can i make the data visible to other users?
Explaination of this scenario will be highly appreciated.
By using the setInterval function all users have to reload the comment section every 10 seconds which is a bad practice and not the best setup for your server. It is however the easiest solution.
The best solution is a socket, a socket will notify other users when one user has commented and vice-versa. Have a loot at http://www.socket.io/. You will have to create an app in which all users listen to a specified function, if one user commented the other users will be notified. This is a lightweight way of solving this problem because the comments only will be updated if one user has commented (which sends a signal to the function).
Hope this helps. Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With