Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Connection-Specific variables in nodeJS/

Connection-Specific variables in nodeJS/


I am developing an application in nodeJS which uses 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?

Problem courtesy of: majic bunnie


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.

Solution courtesy of: Mark


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Connection-Specific variables in nodeJS/


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription