Private posts in node js and socket io using php
I am developing a chat app using nodejs, socket.io and php, but I have a simple error in private messaging between two users. Therefore, I use the email id as a unique key for each user.
When user A sends a message to user B, he cannot read the messages until user B sends a message to user A after that, everything will work as expected.
chat.php
<form class="form-inline" id="messageForm">
<input id="messageInput" type="text" class="input-xxlarge" placeHolder="Message" />
<input type="hidden" name="" id="to" value="<?php echo $recevierid; ?>">
<input type="submit" class="btn btn-primary" value="Send" />
</form>
Here the field hidden
contains the recipient's email id.
chat.js
$( "#messageForm" ).submit( function() {
var msg = $( "#messageInput" ).val();
var to =$( "#to" ).val();
socket.emit('join', { message:msg,to:to} );
$( "#messageInput" ).val('');
return false;
});
socket.on( 'new_msg', function( data ) {
var actualContent = $( "#messages" ).html();
var newMsgContent = '<li>: ' + data.message + '</li>';
var content = newMsgContent + actualContent;
$( "#messages" ).html( content );
});
nodeServer.js
var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
io.sockets.on( 'connection', function( client ) {
console.log( "New client !" );
client.on('join', function( data ) {
console.log( 'Message received' + data.message+"email"+data.to);
client.join(data.to);
io.sockets.in(data.to).emit("new_msg",{message:data.message});
});
});
server.listen( 8080 );
+3
source to share
No one has answered this question yet
Check out similar questions: