Hi all,
Nervous first post but really need some help. I'm working on a Web application for p2p communication (both video and text).
The video communication works (some teething issues), but my main issue is getting user socket.id. specifically the user just having connected.
I have tried many things including:
Socket.on("connected", () {
console.log(socket.id);
});
All I get is "undefined". Yet if I run te same console.log code after the page loads I can get it displayed.
Not sure how to work with that.
I want to store the socket.id and username in an object/array
Thank you
Top comments (7)
Websocket is better than
socket.io
. There are sum bugs which causes trouble when the socket code becomes big.I'll keep that in mind. It's a company project so this is just a prototype for now. Thank you
It's okey for that.
socket.on(eventName, listener)
Socket#id
How does that work?
Watch for the casing on the variable names:
Basically the
listener
is a function expression (arrow functions are always function expressions) - and a function has access to all the variables in the scope that created it (in this casesocket
).MDN: Closures
A function declaration
would work as well.
Thank you. I think it was just my phone adding the caps but I'll double check my code. Thank you
I think you should pass in the "socket" param as follow:
Socket.on("connected", (socket) {
console.log(socket.id);
});
I'll give it a go, thank you