8

I'm having trouble with my chat app, I need to be able to send a private message to a specific user, I was able to select that specific user but for some reason couldn't figure out how to send the private message.

Below you will find the code for my server, please help:

var express = require('express');
var app = express();
var PORT = process.env.PORT || 8000;
var http = require('http').Server(app); // this is a node server that uses express as the boiler plate
var io = require('socket.io')(http); // socket! pass our server as a parameter to it


// use express static to expose a folder
app.use(express.static(__dirname + '/public'));

var users = [],
 connections = [];
var onlineClients = {};




// Register events on socket connection
io.on('connection', function(socket){ 
 connections.push(socket);
 // console.log("connected socket", connections);

 socket.on("disconnect", function() {
  users.splice(users.indexOf(socket.username), 1);
  updateUsernames();
  connections.splice(connections.indexOf(socket), 1);
  console.log("disconnected socket", connections.length)
 });

 socket.on("send message", function(data) {
  // console.log(data);
  io.emit("new message", {msg: data, user: socket.username});
 });


 socket.on("notify user", function(data) {
  io.emit("notify user", {user: socket.username})
 });


 socket.on("new user", function(data) {
  socket.username = data;
  users.push(socket.username);
  updateUsernames();

 });

 function updateUsernames() {
  io.emit("get users", users);
 };

 socket.on("private", function(data, recipientName) {
  var recipient = connections.filter(function (recipient) {
   return recipient.username === recipientName;
  })[0];
   console.log(recipient.id);
   console.log(data);
  io.sockets.socket(recipient.id).emit("received private msg", data);

 });


 // socket.on("create room", function(room) {
 //  socket.join(room);
 //  io.sockets.in(room).emit('event', "hey wusup am in this room");
 //  console.log(socket);

 // })

});

http.listen(PORT, function(){
  console.log('Server started on port ' + PORT);
});
cickStar
  • 119
  • 1
  • 2
  • 8
  • make a private room then send message to a particuler user in that room – Adiii Nov 29 '16 at 07:07
  • 2
    Clients only send message to the server. If you want the client to get a message to a particular user, then you send a message to the server and you tell the server which user you want the server to deliver it to. – jfriend00 Nov 29 '16 at 07:18

2 Answers2

11
  • First add user in chat room so that will easy to find a user in your private chat room
  • Your client side code for join private room

    <input type="text" class="form-control" id="user_email" placeholder="user_email"  />
    <button text="join room" class="btn btn-primary btn-block" onclick="a();"> Join Room</button>
    
  • your javascript code in client side

    function a(){
        io.emit('privatechatroom', {email:document.getElementById('user_email').value});
    }
    
  • your server side code to add user in your room

    socket.on('privatechatroom',function(data){
        socket.join(data.email);
        io.emit('res',{mes:"you are added"})
    });
    

now u can send private message to that person that is recently addedd to this room client side

function b() {
    io.emit('sendmail', { email: document.getElementById('sender_mail').value, message: document.getElementById('message').value });
    $('#message').val('');
}
/*serverside code*/
socket.on('sendmail', function (data) {
    io.sockets.in(data.email).emit('new_msg', { msg: data.message });
    console.log(data.email);
});
Art
  • 35
  • 4
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • 2
    thanks Adil for help but not sure this can work for my case, I'm sending the private message to the client by adding a click event to that specific user, I was able to get the id but can't figure out how to send the message, I tried different things, I know I should change something on the server side : – cickStar Nov 29 '16 at 08:30
0

Here is the clear solution of mine for your question...

Send a message to Particular client(Private chat)

I hope it will work for you sue..

Community
  • 1
  • 1
Trojan
  • 1,396
  • 2
  • 16
  • 19