0

I have a global variable (openTokSessionID) that is set by a function. When the function is run again, it should check to see if openTokSessionID is set. If it is, it should use the current value. Instead, however, it complains that openTokSessionID is undefined. My server code is below. Thanks for your help.

var http = require('http').createServer(handler), 
io   = require('socket.io').listen(http).set('log level', 1),
opentok = require('opentok'),
key = '',    // Replace with your API key  
secret = '';  // Replace with your API secret  
var ot = new opentok.OpenTokSDK(key,secret);
ot.setEnvironment("staging.tokbox.com");
//ot.setEnvironment("api.opentok.com"); //only for production
http.listen(8080);
console.log('Chatserver listening on port 8080');

var nicknames = {};
var log = {};
var connectedUsersObject={};
var privateSessionObject={};
var data;
var openTokSessionID='';


function handler(req, res) {
  res.writeHead(200);
  res.end();
}

////////////////////////////////////////////SOCKET.IO FUNCTIONS///////////////////////////////////////////////////////
io.sockets.on('connection', function (socket) {
    socket.on('private message', function(data) {
        console.log('OpenTok Session ID is: ....' + openTokSessionID);
        if(data.messageType=='openTokDemoRequest'){ //if new session, create unique session ID, add to current chat object
            console.log(data.messageType + ' sender: ' + data.from);

            var location = '127.0.0.1'; // use an IP or 'localhost' 
            console.log('message type is: "openTokDemoRequest". openTokSessionID is: ' + openTokSessionID);
            var messageRecipient=data.from;
            if(openTokSessionID==''){
                console.log('The session ID is: ' + openTokSessionID + '++++++++');openTokSessionID= ot.create_session(location, function(openTokSessionID){

                    var data= {'to':messageRecipient, 'message':{'token': ot.generate_token({'session_id':openTokSessionID, 'role': "publisher"}), 'sessionID': openTokSessionID, 'apikey':key}, 'from': 'openTok', 'messageType':'demoTokenInfo', 'privateSessionID':''};
                    console.log('NEW session id is: ' + openTokSessionID + '. token is: ' + data.message.token);
                //  privateMessageSend(data);
                //  sendToUser(data);
                    connectedUsersObject[messageRecipient].emit('private message', data);
                    console.log ('message recipient is: ' + messageRecipient);
                }); 
            }
            else{
                console.log('OpenTok Session ID is: ////' + openTokSessionID);
                var data= {'to':messageRecipient, 'message':{'token': ot.generate_token({'session_id':openTokSessionID, 'role': "publisher"}), 'sessionID': openTokSessionID, 'apikey':key}, 'from': 'openTok', 'messageType':'demoTokenInfo', 'privateSessionID':''};
                console.log('session id is: ' + openTokSessionID + '. token is: ' + data.message.token);
                connectedUsersObject[messageRecipient].emit('private message', data);
                console.log ('message recipient is: ' + messageRecipient);
            }   

        }
    });
5
  • either I don't understandn Javascript scope, or I made a dumb error :) Commented Aug 10, 2012 at 3:10
  • Which statement is generating that error? Commented Aug 10, 2012 at 6:04
  • console.log('OpenTok Session ID is: ....' + openTokSessionID); The second time I run this function, it should show the session id from the first time, but it's showing 'undefined', even though it's initially set to ''. Commented Aug 10, 2012 at 8:09
  • Is that the statement with or without the slashes? Commented Aug 10, 2012 at 11:28
  • There are no slashes in that statement Commented Aug 10, 2012 at 12:15

1 Answer 1

1

Here you're trying to use the return value from an asynchronous call, and as a result you're setting your variable to undefined:

openTokSessionID= ot.create_session(location, function(openTokSessionID){
...

Because you gave your callback argument the same name as your variable, it masked your variable within the callback so it looked OK the first time, but your actual variable got trashed.

You need to change it to something like:

ot.create_session(location, function(sessionID){
  openTokSessionID = sessionID;
...
Sign up to request clarification or add additional context in comments.

1 Comment

That was it - I didin't realize that I couldn't overwrite the variable directly with the callback

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.