Map(Y) => Set(x), socket.io

huangapple go评论61阅读模式
英文:

Map(Y) => Set(x), socket.io

问题

I'm trying to perform some analytics on the sockets that are coming from the server, I want to know which room has the most connections. Cause in one utility event for sockets, I'm emitting the total number of connected sockets for a particular room (guestRoom), and in some instances, this can climb quite high. But this happens whenever someone makes a connection to the socket through the browser and is not logged in through my service. Of these socket events, they are further divided into other rooms based on how they are interacting with the service. The room has the _id of the document as the room name. I want to know which of these rooms has the most connected users.

So my thought process on this is to get all the rooms, loop over the data, count the total number of connections, and send back the id of the room.

Here is the code I've crafted so far:

/**
 * @description finds the room with the most connections and returns the id of the room to the client
 * @param {Object} io - the socket io object
 *
 */
export default async (io) => {
  try {
    const rooms = io.sockets.adapter.rooms;
    console.log(rooms);
    let max = 0;
    let roomWithMostConnections = '';
    for (const room in rooms) {
      console.log(`Room: ${room}`.yellow);
      if (room == 'adminRoom' || room == 'userRoom' || room == 'watcherRoom')
        continue;
      // we need to turn each room into an array and check the length of the connections
      const roomConnections = Array.from(rooms[room].sockets);
      console.log(`Connections: ${roomConnections.length}`.green);
      if (roomConnections.length > max) {
        console.log(`Max: ${max}`.red);
        max = roomConnections.length;
        roomWithMostConnections = room;
      }
    }
    console.log(
      `The room with the most connections is ${roomWithMostConnections}`
    );
    io.emit('mostConnections', roomWithMostConnections);
  } catch (error) {
    console.log(error);
  }
};

The thing that's causing my hang-up is the data that's returned for the variable const rooms, which looks like this:

[0] Map(6) {
[0]   'Sx2LUMA6rhMMJYbXAAAD' => Set(1) { 'Sx2LUMA6rhMMJYbXAAAD' },
[0]   '0Cb2x7XgEsRWgpF2AAAH' => Set(1) { '0Cb2x7XgEsRWgpF2AAAH' },
[0]   'Ye33dgBl3UbJt1WGAAAK' => Set(1) { 'Ye33dgBl3UbJt1WGAAAK' },
[0]   'wCpe0OQQCVqF1Z08AAAL' => Set(1) { 'wCpe0OQQCVqF1Z08AAAL' },
[0]   '61CJKUhmjGP6kbZpAAAO' => Set(1) { '61CJKUhmjGP6kbZpAAAO' },
[0]   'NPxCHhSzypIE_t8nAAAP' => Set(1) { 'NPxCHhSzypIE_t8nAAAP' }
[0] }

I'm not sure how to read this data. I know map usually refers to an array method, but this clearly looks like an object. However, something like Object.entries returns an [] array, and if I do Array.from, the data gets even messier.

What is this data, and how should I read it so I can turn this into an array to count the total number of connections?

英文:

Im trying to perform some analytics on the sockets that are coming from the server, i want to know which room has the most connections. cause in one utility event for sockets, im emitting the total number of connected sockets for a particular room (guestRoom) and in some instances this can climb quite, high, but this happens whenever someone makes a connection to the socket through the browser and is not logged in through my service, of these socket events they are further divided into other rooms based on how they are interacting with the service, the room has the _id of the document as the room name, i want to know which of these rooms has the most connected users.

so my thought process on this is to get all the rooms loop over the data count the total number of connections and send back the id of the room.

here is the code i've crafted so far.

/**
 * @description finds the room with the most connections and returns the id of the room to the client
 * @param {Object} io - the socket io object
 *
 */
export default async (io) => {
  try {
    const rooms = io.sockets.adapter.rooms;
    console.log(rooms);
    let max = 0;
    let roomWithMostConnections = '';
    for (const room in rooms) {
      console.log(`Room: ${room}`.yellow);
      if (room == 'adminRoom' || room == 'userRoom' || room == 'watcherRoom')
        continue;
      // we need to turn each room into an array and check the length of the connections
      const roomConnections = Array.from(rooms[room].sockets);
      console.log(`Connections: ${roomConnections.length}`.green);
      if (roomConnections.length > max) {
        console.log(`Max: ${max}`.red);
        max = roomConnections.length;
        roomWithMostConnections = room;
      }
    }
    console.log(
      `The room with the most connections is ${roomWithMostConnections}`
    );
    io.emit('mostConnections', roomWithMostConnections);
  } catch (error) {
    console.log(error);
  }
};

the thing thats causing my hangup is the data thats returned for the variable const rooms which looks like this:

[0] Map(6) {
[0]   'Sx2LUMA6rhMMJYbXAAAD' => Set(1) { 'Sx2LUMA6rhMMJYbXAAAD' },
[0]   '0Cb2x7XgEsRWgpF2AAAH' => Set(1) { '0Cb2x7XgEsRWgpF2AAAH' },
[0]   'Ye33dgBl3UbJt1WGAAAK' => Set(1) { 'Ye33dgBl3UbJt1WGAAAK' },
[0]   'wCpe0OQQCVqF1Z08AAAL' => Set(1) { 'wCpe0OQQCVqF1Z08AAAL' },
[0]   '61CJKUhmjGP6kbZpAAAO' => Set(1) { '61CJKUhmjGP6kbZpAAAO' },
[0]   'NPxCHhSzypIE_t8nAAAP' => Set(1) { 'NPxCHhSzypIE_t8nAAAP' }
[0] }

Im not sure how to read this data, i know map usually refers to an array method, but this clearly looks like an object, but something like Object.entries returns an [] array, and if i do Array.from the data gets even messier...

what is this data, and how should i read it so i can turn this into an array to count the total number of connections.

答案1

得分: 1

这个地图是一种数据结构,我们可以说它像一个对象,接受任何东西作为键,并按顺序保存其数据。

文档
我建议您还查看JavaScript的完整参考书。

您可以使用Map的get方法检索其数据。

rooms.get('Sx2LUMA6rhMMJYbXAAAD')

地图也是可迭代的,所以您可以使用rooms.forEach(),例如。

如果您不知道的话,Set是另一种数据结构。希望这篇文章对您有所帮助。

地图和Set信息

英文:

This Map is a data structure, we could say its like an object, that accepts anything as a key and saves its data in order.

Docs
I would suggest you also looking at javascripts full reference book.

You can use Map's get method to retrieve its data.

rooms.get('Sx2LUMA6rhMMJYbXAAAD')

Maps are also iterable, so you could use rooms.forEach() for example.

In case you dont know. a Set is another data structure. Hope this article can help you.

Map and Set info

huangapple
  • 本文由 发表于 2023年1月5日 22:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75019955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定