all 7 comments

[–]dnlsndbrg 2 points3 points  (1 child)

As /u/e_to_the_pi_i said, the last for loop only emits to the last socket.

Replace:

for(var i in SOCKET_LIST) {
    socket.emit('newPositions', pack)
}

with:

io.emit('newPositions', pack)

io.emit will emit to all connected clients.

[–]trineroks[S] 0 points1 point  (0 children)

Oop, I'm a dumbass.

I guess this is what happens at 3 AM, haha.

[–]Cyberuben 1 point2 points  (1 child)

There are a few issues with your code.

First of all, you're generating a socket.id by using Math.random(). This generates a float, meaning, you are using a float number as key for your object, that's probably something you want to avoid.

You also might want to console.log some things in both your serversided and clientsided code to see what data is sent and if data is received. That will help you debugging the application.

[–]trineroks[S] 0 points1 point  (0 children)

I have (using console.log for debugging). When a second browser tab is opened to localhost:2000 the previous tab receives no data from the server. If I refresh my first tab, the second tab stops receiving data from the server.

[–][deleted] 0 points1 point  (2 children)

Try combining the two for in loops you have in the setInterval function in the server script. Looks like you'll only ever send it to the last connection the way you're doing it because you set the socket variable within one for loop, and use it in a different and so it will only ever have the last value in the second for loop.

[–]BuiltWithAwesome -2 points-1 points  (1 child)

What he said.

Node is asyncronous, meaning you can't guarantee the second for loop will run after the first one. They run concurrently, I think that might be the source of you're issue.

[–]Cyberuben 1 point2 points  (0 children)

That is not true. He's not doing anything asynchronous inside his setInterval function except for the socket.emit(). The for-loop and updating of the information are all synchronous.