How to get Socket Id in client side #4692
Unanswered
Hrishikesh-ti
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am using React in frontend and I am calling socket.emit('create-room') and after that I want socket id in frontend as variable & not only inside callback function.
eg:-
let socket = io.connect('http://localhost:');
console.log(socket.id); // undefined
socket.on('connect', () => {
console.log(socket.id); // an alphanumeric id...
});
I can get socket.id inside callback function but I want it as a global variable.
like this :-
let socket = io.connect('http://localhost:');
let socketId;
console.log(socket.id); // undefined
socket.on('connect', () => {
console.log(socket.id); // an alphanumeric id...
socketId=socket.id;
});
return socketId;
But it returns "undefined" how to do this .. please help
Beta Was this translation helpful? Give feedback.
All reactions