Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection-Specific variables in nodeJS/Socket.io

I am developing an application in nodeJS which uses socket.io for interactive gameplay. Inside the socket.on('connection') callback block, I have declared several variables. From what I can tell, these variables are connection-specific and there will be an instance for each callback fired on a socket connection for each client.

During my tests, I tried in two browsers and was able to play two independent game sessions at the same time which lead me to confirm this view of those variables.

However, when in a more live environment, I was noticing other players actions appearing as actions on my screen. I don't use broadcast anywhere, just simple emits inside the connection block, so I am perplexed why I see this happening.

Is my assumption about connection-specific variables incorrect? Or is there a more subtle application-specific reason this might be happening?

like image 618
majic bunnie Avatar asked Mar 13 '26 23:03

majic bunnie


1 Answers

Your assumption is correct - the variables are local to the per-socket connection event. There is something else going on which is causing you issues.

like image 76
Mark Avatar answered Mar 16 '26 14:03

Mark