Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm stuck in this project.
How do I scroll down the chat room height after a new message?
I've tried a lot of code but, I failed to get the right code.

I upload an video for watch the problem

i hope you understand it now


What I have tried:

<div class="col-lg-9 col-xl-10">
<div id="boxz" class="chat-box scrollable position-relative">
<!--chat Row -->

<ul class="chat-list list-style-none px-3 pt-3" id="mds">


</ul>

</div>


<div class="card-body border-top chat-box">
<div class="row">
<div class="col">
<div class="input-field mt-0 mb-0">

<form id="chatroom" enctype="multipart/form-data">
<div class="fas fa-paperclip"></div>
<div class="fas fa-microphone record"></div>
<div class="input-group">
<textarea class="form-control" name="mgs" id="messages"
placeholder="Type and enter"></textarea>
<div class="input-group-prepend">
<span class="input-group-text">
<a class="btn-circle btn-lg btn-cyan float-right text-white"
href="javascript:void(0)" id="btn_chat"><i
class="fas fa-paper-plane"></i></a>
</span>
</div>
</div>
<input type="file" name="images[]" id="images"
style="display:none"
accept="image/x-png,image/gif,image/jpeg" multiple />
<input type="hidden" id="client_ids" name="client_ids">
</form>

</div>
</div>

</div>
</div>





/**************** Update All chatbox and Right ********************** */
setInterval(() => {

/** Collect Client Chat data */
var id = $('#client_ids').val();
userid(id);

/*** Right Side Client Area */
UpdateList();

}, 3000); /*** setInterval function */


/****** Chatbox Right Side bar *******/
function UpdateList() {
$('#rightsidebar').load('./core/right_side').fadeIn('slow');
}
/******************************************/


/****** Fetch Client chat data by click client name */
function userid(id) {
$('#client_ids').val(id);
$.get("./core/chat_fetch", {
client_code: id
}, function(data) {
$('.chat-list').html(data);
$(".chat-box").animate({
scrollTop: $(".chat-box")[0].scrollHeight
}, 0);
});
}
/*******************************************/



$(document).ready(function() {
UpdateList();
$("#messages").emojioneArea({
pickerPosition: "top",
tonesStyle: "bullet",
events: {
keyup: function(editor, event) {
// console.log(editor.html());
// console.log(this.getText());
// $('#gts').html(editor.html());
}
}

});
});
Posted
Updated 10-Aug-21 23:09pm
v2
Comments
Richard Deeming 11-Aug-21 4:21am    
Nobody can help you, because we have no idea how your code would identify that there is a new message.

Remember, we can't see your screen, access your computer, or read your mind. All we have to go on is what you type in the question box.

Click the green "Improve question" link and update your question to include a proper description of the problem.

1 solution

Richard is correct, the AJAX request you're performing seems to hit an API endpoint which produces the HTML content for the chat. What this doesn't do is give you any indication of whether a new message is present, how would your JS even know if there is one?

What you could do is have the endpoint produce a JSON response containing the list of messages, and then have your JS build the chat view. This can be done either manually or by using a templating engine. You could then track the most recent message by ID or timestamp, and make the page scroll when a newer is detected.

An alternative, and perhaps a bit of a hack, would be to have the API continue returning the HTML but also return a custom header containing the ID or timestamp of the last message. Similar to what I mentioned previously, you could then consume this header and keep track of it to work out whether new messages are present. For example, X-Latest-Message-ID
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900