Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8390925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:07:32+00:00 2026-06-09T19:07:32+00:00

I have a global variable (openTokSessionID) that is set by a function. When the

  • 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);
            }   

        }
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:07:33+00:00Added an answer on June 9, 2026 at 7:07 pm

    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;
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So in jQuery, I have a global variable currentSubNav that stores a current visible
I have global variable fan_coil_ai_bi, and I use that variable to store some data
I have a global variable in my javascript, whose value I want to set
In Java, I have a global variable x, what should I use to allows
I have a global variable p. I have a function that draws (or redraws)
If i have global variable in A.dll, that depends on global variable in B.dll
I have a global variable averagel that I am trying to manipulate in a
I have a global variable that is an instance of my custom class. How
I have a vector declared as a global variable that I need to be
I have a global variable I called Y_VAL which is initialized to a value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.