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

  • SEARCH
  • Home
  • 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 7643441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:27:00+00:00 2026-05-31T09:27:00+00:00

I want to build a chat system in nodeJs + MYSQL using php. It

  • 0

I want to build a chat system in nodeJs + MYSQL using php. It will be private chat one to one and will save chat in database. Anyone know from where I need to start.

Currently I got this code for SERVER:

var app = require('express').createServer()
var io = require('socket.io').listen(app);

app.listen(8181);

// routing
app.get('/', function (req, res) {
 res.sendfile(__dirname + '/index.html');
});

// usernames which are currently connected to the chat
 var usernames = {};

 io.sockets.on('connection', function (socket) {

// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
    // we tell the client to execute 'updatechat' with 2 parameters
    io.sockets.emit('updatechat', socket.username, data);
});

// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
    // we store the username in the socket session for this client
    socket.username = username;
    // add the client's username to the global list
    usernames[username] = username;
    // echo to client they've connected
    socket.emit('updatechat', 'SERVER', 'you have connected');
    // echo globally (all clients) that a person has connected
    socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
    // update the list of users in chat, client-side
    io.sockets.emit('updateusers', usernames);
});

// when the user disconnects.. perform this
socket.on('disconnect', function(){
    // remove the username from global usernames list
    delete usernames[socket.username];
    // update list of users in chat, client-side
    io.sockets.emit('updateusers', usernames);
    // echo globally that this client has left
    socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has         disconnected');
});
  })
  • 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-31T09:27:01+00:00Added an answer on May 31, 2026 at 9:27 am

    There are two ways. Thirst you can hold references to all sockets in an array (all at least IDs of these sockets). When a user emits private message you search the array for target socket and send it to this particular one. This requires to hold some kind of ID of a socket. You may use inner socket.id but it will be a problem when the client reconnects (new ID generated). And there is another problem when your app works on more then one machine (they cannot share arrays of connected clients).

    The second way is to use rooms. Whenever client connects I suppose he has a name, for example John. Then you can use something like this for his connection:

    socket.join('/priv/'+name);
    

    Now this creates a room and adds socket to it. If you want to send message to John then you simply use

    io.sockets.in('/priv/John').emit('msg', data);
    

    At that point you can be sure that the message went exactly to the socket in /priv/John room. This works perfectly with Redis combined with socket.io (to avoid many machines problem) and session authorization. I didn’t try it with memoryStore, but it should work as well.

    Also you don’t have to worry about rooms when clients disconnect. Socket.io automatically destroys empty rooms.

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

Sidebar

Related Questions

I want to build a chat server but i don't know i should use
I want to build two-dimentional array of strings where length of one dimention is
I want to build a simple web-interface application that can send/receive chat messages to/from
I am trying to build a bluetooth chat application using j2me. I created a
I'm trying to build a site that runs without refreshes, using Jquery and PHP
i want build a blog using jekyll and octopress. but an error happened when
I want to build a dynamic ListView (like a chat list with different layouts/bubbles).
Hai I want to build a web site containing chat. i am working with
Possible Duplicate: Using memcached as a database buffer for chat messages A friend of
I want build a search service for one particular thing. The data is freely

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.