I'm using a node.js server with OpenTok. I'm creating private chats between two users, and want to create one session and two tokens. The problem is that the generate_token function is only creating one token, then sending that to both users. In this code, I want to send the individual tokens to each user.
My code is below:
var sessionId;
var sourceUser=data.from;
var targetUser=data.to;
if(data.messageType=='accepted'){
var location = '127.0.0.1'; // use an IP or 'localhost'
ot.create_session(location, function(result){
sessionId = result;
});
console.log('chat has been accepted by ' + data.from + '. I can start the OpenTok session now with ' +data.to + '.');
var data= {'to':sourceUser, 'message':{'token': ot.generate_token({session_id:sessionId, 'connection_data': Math.floor(Math.random()*999999),
'role': "publisher"}), 'sessionID': sessionId, 'apikey':key}, 'from': targetUser, 'messageType':'tokenInfo'}
connectedUsersObject[sourceUser].emit('private message', tstamp(), socket.nickname, data);
console.log(sourceUser + '\'s token is: ' + data.message.token + '. The sessionID is: ' + data.message.sessionID + '. The API key is: ' + data.message.apikey);
console.log('connection data is: ' + data.message.connection_data);
var data= {'to':targetUser, 'message':{'token': ot.generate_token({session_id:sessionId, 'connection_data': Math.floor(Math.random()*999999),
'role': "publisher"}), 'sessionID': sessionId, 'apikey':key}, 'from': sourceUser, 'messageType':'tokenInfo'}
connectedUsersObject[targetUser].emit('private message', tstamp(), socket.nickname, data);
console.log(targetUser + '\'s token is: ' + data.message.token + '. The sessionID is: ' + data.message.sessionID + '. The API key is: ' + data.message.apikey);
console.log('connection data is: ' + data.message.connection_data);
The entire server code is at: http://pastebin.com/uvWfKSWp