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 6098415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:09:20+00:00 2026-05-23T13:09:20+00:00

I am currently developing a chat system using Node.js. My question is, how do

  • 0

I am currently developing a chat system using Node.js. My question is, how do I handle multiple chat sessions with Node.js. That’s a bit vague so I’ll explain…

So for example two people might engage in a chat session. Node.js checks every couple of seconds for new messages. This is simple enough as I can get this to work fine using my code below.

My issue is when another two people at the same time are in another chat session.

At present I am sending two query strings called “chat_id” and “check_date” so it can check for new chat messages after a certain date and time.

Problem is, the “chat_id” and “check_date” are overwritten every time a new chat is started because the server is being used by both chat rooms.

Am I making sense?

My code below:

var chat_id, check_date;

var sys = require("sys"),  
    http = require("http"),  
    url = require("url"),  
    path = require("path"),  
    fs = require("fs"),  
    events = require("events");  

// Conntect to database
var Client = require('mysql').Client;
var client = new Client();
client.host = 'localhost';
client.port = 3306;
client.database = '****';
client.user = '****';
client.password = '****';
client.connect(function(error, results) {
  if(error) {
    console.log('Connection Error: ' + error.message);
    return;
  }
  console.log('Connected to MySQL');
});

// Check for chat updates
function getChatUpdates() {  

    if (typeof(check_date) == 'undefined')
        return;

    client.query('SELECT name, message FROM chat WHERE chat_id = "' + chat_id + '" AND date_entered > "' + check_date + '"', 
    function selectCb(error, results, fields) {
        if (error) {
            console.log('GetData Error: ' + error.message);
            client.end();
            return;
        }

        if (results.length > 0) {
            for(var i = 0;i<results.length;i++) {
                console.log('Name: ' + results[i]['name']);
                console.log('Message: ' + results[i]['message']);
            }
        }

  });

}  

// Set interval to check for chat updates every 2 seconds
setInterval(getChatUpdates, 2000);  

http.createServer(function(request, response) {  

    // Get the query strings
    var uri = url.parse(request.url, true);  
    chat_id = uri.query.chat_id;
    check_date = uri.query.check_date;
    console.log('Chat ID: ' + chat_id);
    console.log('Last Check: ' + check_date);

    // we'll do something here later.

}).listen(8080);  

sys.puts("Server running at http://localhost:8080/");
  • 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-05-23T13:09:21+00:00Added an answer on May 23, 2026 at 1:09 pm

    It seems like you are having a PHP background and when programming in node.js you should have a completely different mindset. You should not poll(setInterval(getChatUpdates, 2000);
    ) for messages, but push(callbacks/events) messages to users on event. I advice you to have a look at socket.io and use a in-memory database like redis(instead of mysql) to store your messages. When you code like this your site will perform much better and is going to be real-time(more). Also I would advice you to use express as your framework to develop websites instead of using raw http module. I don’t know if you already know about npm, but you should use that to manage your dependencies.

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

Sidebar

Related Questions

I am currently developing a Rails application using a database that was designed before
I'm currently developing a PHP application that's using an Access database as a backend.
I was currently developing a desktop application in C# using mono and testing in
I am currently developing an approval routing WCF service that will allow an user
I'm currently developing an application that is comprised of five separate executables that communicate
I'm developing a webchat system which shows the current 'members' of the chat as
I'm currently working with a dead network application that received packets for a chat.
Currently developing an application using the newest version of symfony, obtained through PEAR. This
Currently developing a connector DLL to HP's Quality Center. I'm using their (insert expelative)
Im currently developing a system where the user will end up having large arrays(

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.