If i do console.log(socket) i get a socket object in firebug. In the obj I could see a property with id
and i could see the value of the id. But when I do console.log(socket.id) i get undefined. why?
var socket = io();
$(document).ready( function(){
console.log(socket);
console.log(socket.id);
console.log(socket.ids);
$(".click").on("click", function(e){
alert("clicked")
socket.emit("clicked", socket.id)
$(this).addClass("removeclick");
})
});
ps I could get socket.ids
which is 0 but not socket.id.
If i do console.log(socket) i get a socket object in firebug. In the obj I could see a property with id
and i could see the value of the id. But when I do console.log(socket.id) i get undefined. why?
var socket = io();
$(document).ready( function(){
console.log(socket);
console.log(socket.id);
console.log(socket.ids);
$(".click").on("click", function(e){
alert("clicked")
socket.emit("clicked", socket.id)
$(this).addClass("removeclick");
})
});
ps I could get socket.ids
which is 0 but not socket.id.
2 Answers
Reset to default 16Socket.io needs some time for establish the connection. The best way I found to get ID on client-side is:
socket.on('connect', () => {console.log(socket.id)});
'connect' is system event which emitting when connection is ready.
(my current socket.io version is 1.7.2)
set the port Lister and get the id via anonymous function 'http://localhost:8000'
this.socket = io('http://localhost:8000');
this.socket.on('connect' , () => {
console.log(this.socket.id);
});
console.log()
in firebug it works as expected. Have you tried that? – slebetman Commented Aug 19, 2015 at 8:35console.log(JSON.stringify(socket))
and I think you'll understand what's happening. – slebetman Commented Aug 19, 2015 at 8:36console.log(JSON.stringify(socket))
I get TypeError: cyclic object value. Is that what I am supposed to see? – jack blank Commented Aug 19, 2015 at 8:47console.log()
was called. But the reason you can see a connected socket object is that the console displays a live reference of that object so what you're seeing is actually a future version ofsocket
– slebetman Commented Aug 19, 2015 at 9:14socket.id
in asetTimeout()
. – slebetman Commented Aug 19, 2015 at 9:14