Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9147693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:03:35+00:00 2026-06-17T11:03:35+00:00

hello i have developed the chat application using socket.io, expressjs and mongoose it works

  • 0

hello i have developed the chat application using socket.io, expressjs and mongoose it works fine. it refreshes after some seconds and fetches new clients from db if exist. problem is that user can feel that div is refreshsing.and also some time take soem time in response. how to avoid this case. here is my code
This is my server side code

  setInterval(function () {
  var allOnLine;
  allOnLine = io.sockets.clients();
   for (var client in allOnLine) {
    if (allOnLine[client].username == "undefined") {
    continue;
    } else {
    notifyAll(allOnLine[client].username);
    }
 }
 }, 50000);

and here is notify all method

function notifyAll(userName) {
      contactModel.find({'userId':userName}, (function (err, contactModel) {
         usernames = [];
         var contacts = contactModel;
         for (var a = 0; a < contacts.length; a++) {
            usernames[a] = contacts[a].contactId;
         }
         var allOnLine;
        allOnLine = io.sockets.clients();
        for (var client in allOnLine) {
            if (allOnLine[client].username == "undefined") {
                continue;
            } else {
                for (var i = 0; i < usernames.length; i++) {
                    if (allOnLine[client].username == usernames[i]) {
                        usernames[i] = usernames[i] + " -On";
                    }

        allOnLine[client].username);
                }

            }
        }
        io.sockets.to(userName).emit('updateusers', usernames);

        }));
    }

This is my client code

socket.on('updateusers', function(usernames) {
     jQuery('#usernames').html('');
     jQuery.each(usernames, function(key, value) {
        jQuery('#usernames').append('<div class="chatContact" id="chatLink" onclick="privateChat(\''+value+'\')">' );
}}

any help i had also posted this question but no answer

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:03:36+00:00Added an answer on June 17, 2026 at 11:03 am

    Your problem is that youdelete everything from usernames and after that you write all contacts. You would better remove the offline contacts from the $(‘#usernames’) and after that to add to that list the online contacts. I writed some functions to show you the functionality. I created html list of online contacts and I also created an array of new online contacts. Here is the code:

    <div id="u">
      <div class="d" onclick="chat('asd1')">asd1</div>
      <div class="d" onclick="chat('asd12')">asd12</div>
      <div class="d" onclick="chat('asd13')">asd13</div>
      <div class="d" onclick="chat('asd142')">asd14</div>
    </div>
    

    Here you have the javascript that you need to run after the DOM is ready:

    var onlineUsernames = ["asd211","asd12","asd13","asd14"];
    var usernamesContainerID = 'u';
    var $usernamesContainer = $('#'+usernamesContainerID);
    function extractUsernameFromAttr(onclickValue)
    {
      return onclickValue.split("'")[1];
    }
    function buildExistingUsernames($userDivs)
    {
        var existingUsernames = [];
      $userDivs.each(function(index,value){
        var username = extractUsernameFromAttr($userDivs[index].getAttribute('onclick'));
        existingUsernames.push(username);
      })
      return existingUsernames;
    }
    function removeUserFromList($user)
    {
         document.getElementById(usernamesContainerID).removeChild($user);
    }
    function addUserToList(value)
    {
        $('<div/>',{
             onclick:"chat('"+value+"')",
             class :'d',
             text:value
       }).appendTo($usernamesContainer); 
    }
    
    function deleteOfflineContacts(existingUsernames,usernames,$userDivs)
    {
      $.each(existingUsernames,function(index,value)
      {
         if($.inArray(value,usernames)==-1)
         {
            removeUserFromList($userDivs[index]);
         }       
      })
    }
    function addOnlineContacts(existingUsernames,usernames)
    {
     $.each(usernames,function(index,value)
      {
         if($.inArray(value,existingUsernames)==-1)
         {
            addUserToList(value);
         }         
      })
    
    }
    function update($userDivs)
    {
      var existingUsernames = buildExistingUsernames($userDivs);
      deleteOfflineContacts(existingUsernames,onlineUsernames,$userDivs);
      addOnlineContacts(existingUsernames,onlineUsernames);  
    }
    var $userDivs = $usernamesContainer.children("div");
    setTimeout(function()
    {
       update($userDivs);
    },3000);
    

    If you need it here is a working example: http://jsfiddle.net/9gRyQ/2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have developed my first application for Android using the Ice Cream Sandwich
I have developed the Hello World Android application using Eclipse. I can run the
Hello we have developed our first enterprise app recently. We are using the In-House
I have developed a haskell application which is tested with WinHugs interpreter working fine
I have created a simple Hello World Application using Flash Builder Burrito SDK. I
Hello friends i have developed game application on facebook. I have to post random
I have developed an simple android application that says hello. Now i have transferred
Hello I developed a multi-threaded TCP server application that allows 10 concurrent connections receives
hello I am a wordpress theme developer. i created a theme which works fine
I have developed a desktop database application. But the interface is not rich. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.