Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Socket io: Cannot for loop or use array methods on socket.rooms

I want to write:

socket.on("create-room", (roomID)=> socket.room = roomID)

let userS_selected_room = 'f2eac135-eafd-49e1-adc7-c89351703896';

for(room in socket.rooms){
  if(room === userS_selected_room) {do_stuff()}
}

I don't understand these: Map(4), [Set]

console.log(socket.rooms)

Set(4) {
  'zBROv1Lug0XhoQxCAAAB',
  'room1',
  '9e9ecaa6-473a-43a6-9ab7-60ff034ab614',
  '1dc1547c-d265-4d5a-bd3f-9a5d37bf883a'
}

console.log(socket)/

...
    rooms: Map(4) {
      '6Otk--hk5SHOVRcrAAAD' => [Set], //socket id 
      'room1' => [Set],
      'f2eac135-eafd-49e1-adc7-c89351703896' => [Set], //this is room id
      '91fdf074-a1e7-493b-97b9-5a6050095697' => [Set] //this is room id
    },
...
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

That output is telling you that socket.rooms is a Map object, which maps keys (like '6Otk--hk5SHOVRcrAAAD') to Set objects. You can loop through the map using for-of (not for-in):

for (const [roomid, room] of sockets.rooms) {
    // ...here, `roomid` will be the ID of the room, and
    // `room` will be a `Set`...
}

If you don't need the room IDs, you can use the values method and loop its result:

for (const room] of sockets.rooms.values()) {
    // ...here, `room` will be a `Set`...
}

Or if you have a room ID, get the Set for that room via get:

const room = socket.rooms.get(roomID);

You'll have to refer to whatever library you're using to populate socket.rooms in order to find out what the Set objects contain, but to loop through the elements in the set, you'd use for-of again:

for (const whatever of room) {
    // ...use `whatever`...
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!