0

How to figure out this problem please ?

My question is related to sockets, you have to own some knowledge on it in order to answer please.

I am trying to save socket.id on a variable declared outside of the function itself, but without success ... used even array but same thing.

Here is my code:

var express = require('express');
var app = express();
var http = require('http').Server(app);

var io = require('socket.io')(http)

app.use('/', express.static('www'));

http.listen(3000, function(){
    console.log('listening on *:3000');
});

var token_user = {};
io.on('connection', function(socket){
    token_user["something"] = socket.handshake['query']['something'];
    token_user["socket_id"] = socket.id;

});
console.log(token_user["socket_id"]); // Undefined

I got a message as it's: UNDEFINED.

Any solution or other way to get socket.id outside of there please in order to use it in other function ?

  • It looks like the console.log is executing before the fields are set. – Anthony Jul 16 '18 at 20:34
  • @Anthony i need to use `socket.id` out of scope, not only inside `io.on('connection', function(socket){});` – Ben Marson Jul 16 '18 at 20:39
  • Whats your usecase here? – Jonas Wilms Jul 16 '18 at 20:40
  • @JonasW. I need `socket.id` to store it on redis in order to send messages to a specific users. – Ben Marson Jul 16 '18 at 20:43
  • @JonasW. It's not same question and have no relation with that ! we are speaking about sockets here and already did that with no success ... – Ben Marson Jul 16 '18 at 20:53
  • 2
    I don't think you understand why this question was marked duplicate from your other comments. The main thing is js is asynchronous. This means that `console.log(...` in your code is executed before `io.on("connection",...` You only set the value of socket_id after a connection is made. Therefore, you get this error. Your variable will be accessible out of scope of this `io.on...` but it'll only be defined after a connection. You can use it [like this](https://gist.github.com/aaayushsingh/d75c614ac0c16734a756921418e3ff2d). I will also edit the answer below to give more clarity to you. – itaintme Jul 16 '18 at 21:50
  • ... so basically your code works, except of the `console.log` – Jonas Wilms Jul 17 '18 at 07:20
  • @JonasW. The code works well, i want to get the `socket.id` inside `io.on("connection",...` to use it elsewhere ... so basically you didn't understood my question and this one have nothing related to the one you suggested as duplicated. You must notice that `io.on("connection",...` is not a function but a listner when a new socket connection is entered ... please read more about sockets to understand well my question or try small example by googling. – Ben Marson Jul 17 '18 at 19:30
  • Since my problem is solved by using `REDIS` itself to save it and get it in other places of the script without requiring to use it inside it ... your solution solve nothing since it still require me to use everything inside `io.on("connection",...`, and it's clearely noticied in the post and even the main goal of my question is to use it outside of it. – Ben Marson Jul 17 '18 at 19:30

1 Answers1

0

It looks like the console.log is executing before the fields are set on token_user. The method you pass into io.on is executed when that event occurs. The line after io.on is synchronously executed.

Try moving the console.log into the function you passed into io.on()

function anotherMethod(user) {
    console.log(token_user["socket_id"]); // will be "x"
}

var token_user = {};
io.on('connection', function(socket){
    token_user["socket_id"] = "x"
    anotherMethod(token_user);
});
itaintme
  • 1,575
  • 8
  • 22
Anthony
  • 1,667
  • 2
  • 17
  • 28
  • You didn't got my question sir, i need to get `SOCKET_ID` out of scope, outside of `io.on('connection', function(socket){});` to use it elsewhere. – Ben Marson Jul 16 '18 at 20:38
  • The field will only be available after it's been set. You can call another method to handle your additional logic after the field has been set. – Anthony Jul 16 '18 at 20:39
  • Please give an example on how to get the `socket.id` outside in order to use it elsewhere, please read again my question, it's not as basic as that :). Thank you again for your efforts. – Ben Marson Jul 16 '18 at 20:41
  • @BenMarson I've updated my answer with an example passing the token_user object to another method. – Anthony Jul 16 '18 at 20:42
  • It's not possible in my case since i need to use it outside of functions, in order to use it with `REDIS CLIENT` exactly, since redis have some problems when it's included inside `io.on('connection', function(socket){});`, then i need only `socket.id` to be extracted from that function and use it freely elsewhere. – Ben Marson Jul 16 '18 at 20:45
  • @BenMarson That doesn't sound correct. Anything in Javascript can be used in a function. Maybe search for an example or create another question related to that. – Anthony Jul 16 '18 at 20:47
  • My question is not about that sir, please read it well to understand my goal here, it's clear i think ... extract `socket.id` out of the scope ... client listner or event listner can't be used inside functions. – Ben Marson Jul 16 '18 at 20:48
  • @BenMarson read the answers in question marked as a duplicate. That's the only way to do it. – Anthony Jul 16 '18 at 20:51
  • There no relation between my question and that one marked as duplicated ! – Ben Marson Jul 16 '18 at 20:52
  • @BenMarson Actually it is. The entire scope outside that function has already executed before any fields are set on `token_user`. It's due to synchronous vs asynchronous execution. – Anthony Jul 16 '18 at 21:12
  • @Ben Marson I have updated this answer. Maybe it'll be a little clearer to you – itaintme Jul 16 '18 at 21:54
  • It's all clear but you must know that i don't want show or use it inside the main function, but outside of it ... i want to store on a String and use it outside ... what you did is just basic and already know that, you just create new function and it last an itteration only and executed inside the main one and that not solve my problem ... since like i already noticied it, an event listner can't be integrated inside function. The one way to do it is to store socket.id inside localstorage or session and invoke it outside of it. – Ben Marson Jul 16 '18 at 23:34