3

I have implemented chat application using this (https://github.com/jdutheil/nodePHP),but now i want private chat between two users,but i don't know how to implement it.Please help me to solve the following issue.

When a user logged into his account it will list(using hyperlinks with friends name) his friends like Facebook with a unique id associate with it,on clicking each item it opens new chat page with a text box and a button and start chat.Here is the code

start-chat-with-friends.php

<?php
  //uniqueid of the friend
  $id=$_GET['id'];
?>
<form class="form-inline" id="messageForm">
                <input id="messageInput" type="text" class="input-xxlarge" placeHolder="Message" />
                <input type="submit" class="btn btn-primary" value="Send" />
            </form>

chatclient.js

$( "#messageForm" ).submit( function() {
var msg = $( "#messageInput" ).val();
socket.emit('join', {message:msg} );
    $( "#messageInput" ).val('');
});



chatServer.js**

socket.on('join', function( data ) {

io.sockets.emit('new_msg'+data.to,{message:data.message});
    });

1 Answer 1

3

hi root this might be helpful for u

var users = {};
var sockets = {};

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

// Register your client with the server, providing your username
socket.on('init', function(username) {
    users[username] = socket.id;    // Store a reference to your socket ID
    sockets[socket.id] = { username : username, socket : socket };  // Store a reference to your socket
});

// Private message is sent from client with username of person you want to 'private message'
socket.on('private message', function(to, message) {
    // Lookup the socket of the user you want to private message, and send them your message
    sockets[users[to]].emit(
        'message', 
        { 
            message : message, 
            from : sockets[socket.id].username 
        }
    );
});
});
Sign up to request clarification or add additional context in comments.

1 Comment

i have already implemented it,but i need private chat between the two users

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.