Why socket.id is undefined in the browser

If I do console.log (socket), I get a socket object in firebug. In obj, I could see the property with id , and I could see the id value. 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.

+8
javascript express
source share
2 answers

Socket.io takes some time to establish a connection. Best way to find client side id:

 socket.on('connect', () => {console.log(socket.id)}); 

'connect' is a system event that emits when a connection is ready.

(my current version of socket.io is 1.7.2)

+6
source share

set the Lister port and get the identifier using the anonymous function ' http: // localhost: 8000 '

 this.socket = io('http://localhost:8000'); this.socket.on('connect' , () => { console.log(this.socket.id); }); 
0
source share

All Articles