0

I'm writing a chat for my site in ajax and jquery, I created a jquery function that repeats every 3 seconds to retrieve new messages, but if on the same browser I open any other window on my site at the same time as that of my site with the chat I don't get back the data, that is, I don't see the message

$('#div-chat-more').ready(function() {
  setInterval(function() {
    var listamessg = $(this).attr('data-listamessg');
    var urltot = "/cffunctions.cfc?method=SalesMorechat&lstmsg=" + listamessg
    $('.div-chat-more').val('');
    $.ajax({
      type: "GET",
      url: urltot,
      success: function(data) {
        if (data != "") {
          /*$('html,body').animate({
                scrollTop: $(window).scrollTop() + 100
                }, 500);*/

          $('.div-chat-more').append($(data));; /*alert("alert2!")*/
          $('.scroll-chat').animate({
            scrollTop: $('.scroll-chat').scrollTop() + 100
          }, 100);

          /*$('#btn-chat-more').attr('data-page',counter);*/
          /*$('.div-chat-more').hide();*/
        }
        /*else {alert("alertno!");}*/
      },
      error: function() {
        alert("alert2!");
        /*$('#msg-sales-more').show();*/
        $('.div-chat-more').hide();
      }
    });
    return false;
  }, 3000);
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 2
    It's hard to say what the exact issue is, given the lack of error messages. However I would strongly suggest that you re-engineer your site. If you're building a chat system then AJAX polling is the last thing you should be using. It induces unnecessary delays in the UI and floods your server with requests. To build an effective chat system you need to be using Websockets. – Rory McCrossan Mar 26 '20 at 12:01
  • Every tab creates new session. Response will come to tab from which you have sent the request. – HarshaHR Mar 26 '20 at 12:02
  • as @RoryMcCrossan suggests, for chat application, better to use websockets. – HarshaHR Mar 26 '20 at 12:03
  • @RoryMcCrossan thanks, in fact, someone had already told me about the web socket, but I don't know if it is compatible with jquery – Renato Mascolo Mar 26 '20 at 12:06
  • @HarshaHR I send the new message from the same tab that receives it but if at the same time I also opened another tab on the site, everything stops – Renato Mascolo Mar 26 '20 at 12:08
  • jQuery is a JS framework. If it works with JS, it works with jQuery, and websockets works with JS just fine. From personal experience with this, I'd suggest you research SignalR – Rory McCrossan Mar 26 '20 at 12:09
  • 1
    let me help you to debug. Open two tabs, inspect both tabs, check whether the ajax called is made in both tabs are not, You can check ajax call in "network" section. Let me know – HarshaHR Mar 26 '20 at 12:12
  • @RoryMcCrossan I have implemented the socket chat thing. you can check my github, please feel free to give feedback. Happy Coding :-) – HarshaHR Mar 26 '20 at 12:16
  • 1
    `setInterval` doesn't work so well when it's not the active tab, might be relevant to your issue. https://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs – freedomn-m Mar 26 '20 at 12:23
  • @HarshaHR in fact opening two tabs as you say with the chat I noticed that I send on one and the message is seen on the other ... – Renato Mascolo Mar 26 '20 at 12:26
  • I am not clear about the issues. your issue is, you send ajax call from one tab and it doesn't reflect on the other tab( I might be wrong about understanding,correct me). My suggestion is, debug your application, for that,chrome --> inspect --> netwrok, this is the best way to do. For any errors in ajax, first thing is check your console and network – HarshaHR Mar 26 '20 at 12:30
  • @HarshaHR Let me explain: my chat sends messages and shows them in the same tab, and it works well. if I open another tab with the chat in the same browser, the message sent in the first one no longer appears in this one but in the other. the form sends in the once but does not show the message in the once that is displayed in the second – Renato Mascolo Mar 26 '20 at 13:29
  • can you please explain me the functionality of this api. what i mean is purpose of this api ? Do you have any api to fetch all the messages ?? because once you open new tab, it obvious to lose all the messages show in the previous tab. So there is must be any api to show all the previous messages and you must call this as soon as the chat page appears. – HarshaHR Mar 26 '20 at 23:20
  • @HarshaHR sorry I probably explain myself badly because I have bad English. chats are stored in databases, but the problem is not that I don't see the messages on the new page. if I open two tab with the same chat and watch them simultaneously on two screens it happens that by sending the message from the first , then using the submit button from the first, in the first tab the message no longer appears but appears in the second tab.. Both for inserting in the database and for append messages I use an ajax function. – Renato Mascolo Mar 27 '20 at 11:25

0 Answers0