1

enter code hereIm making an nodejs application... let me show my question;;

//id = dinamic string.. "user1", "user2", "userX"  
usersArray = [];
usersArray[id]["socket"] = socket;

When sockets ends, I want to remove [id] from usersArray.. but at this time id is not availabe

socket.on('end', function () {
    ?........?
    socket.end();
});

How to do it?

Filipe Tagliacozzi
  • 1,431
  • 2
  • 20
  • 28

3 Answers3

2

There's socket.id which works well as an identifier for each socket. Then, you can use an associative array instead of usersArray:

var usersArray = {};
usersArray[socket.id] = some_user_data;

You can then remove this entry using delete usersArray[socket.id].

Kos
  • 70,399
  • 25
  • 169
  • 233
  • 3
    [Don't delete elements in an array, splice them out](http://stackoverflow.com/questions/500606/javascript-array-delete-elements). – Elliot Bonneville May 08 '13 at 17:52
  • @ElliotBonneville Rather than that, I'd just use an Object here. I'd edit to make this clear. – Kos May 08 '13 at 17:53
  • But I want to use the identifiers from Database... The people can choose any name, At sockets end I dont have the userId, bcouse I didnt check database to see if he should stay – Filipe Tagliacozzi May 08 '13 at 18:06
  • So make another associative array, and simply set `arr[socket.id] = user_id`, then use it as a lookup whenever needed. On disconnect, clean up both. – Kos May 08 '13 at 18:11
  • I'd do the opposite though - keep `usersArray[socket.id]` and store the database's `user_id` inside. Node.js code often needs to find the user by "his" socket. – Kos May 08 '13 at 18:12
0

Assuming you have access to thisId from inside the socket.on('end') function, you could do something like:

socket.on('end', function () {
    usersArray.splice(thisId, 1);
    socket.end();
});
maaachine
  • 801
  • 1
  • 7
  • 12
  • 1
    Taking the mention `but at this time all I have is socket data` it might be safe to assume that `thisId` is not available. Luckily you can easily create a second or third event handler within a scope where both the `socket` and `thisId` is available. – Roonaan May 08 '13 at 18:04
  • Right there.. Id is not available – Filipe Tagliacozzi May 08 '13 at 18:11
0

Depending on where you add the sockets to your userArray (which seems to be an object rather than an array noticing your examples for id), you could consider just adding a new callback to the socket when you add it to the array

usersArray[someId].socket = socket;

// New callback to just delete the usersArray entry
usersArray[someId].socket.on('end', function() {
   delete usersArray[someId];
});

Leave the current socket.on untouched:

socket.on('end', function () {
    /* No new code here, as the deletion happens
       when the socket is added to usersArray!
     */
    socket.end();
});
Roonaan
  • 1,066
  • 8
  • 11
  • [Don't delete elements in an array, splice them out](http://stackoverflow.com/questions/500606/javascript-array-delete-elements). – Elliot Bonneville May 08 '13 at 17:54
  • @Elliot, see my mention on the array probably not being an array. – Roonaan May 08 '13 at 17:58
  • @Roonaan someId is not available at this time – Filipe Tagliacozzi May 08 '13 at 18:16
  • @Filipe, `someId` must be availabe, as you use it to assign the socket to the `usersArray`. The whole point of this solution, is that you create another `on('end'` in that code block, with the explicit role to only delete the entry in the array. You leave you current `on('end'` untouched. – Roonaan May 08 '13 at 18:21