Reputation: 1665
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});
});
Upvotes: 3
Views: 1073
Reputation: 348
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
}
);
});
});
Upvotes: 3