Skip to content

Property 'allRooms' does not exist on type 'Adapter'.ts(2339) #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alkalox opened this issue Feb 25, 2022 · 5 comments
Closed

Property 'allRooms' does not exist on type 'Adapter'.ts(2339) #444

alkalox opened this issue Feb 25, 2022 · 5 comments

Comments

@alkalox
Copy link

alkalox commented Feb 25, 2022

Using this snippet from the README,

const rooms = await io.of('/').adapter.allRooms();

Got a typescript error that allRooms does not exist on adapter ^^
Please update with what works to get the list of all rooms, across multiple nodes.

Using the version "@socket.io/redis-adapter": "^7.1.0", "socket.io": "^4.4.1",

@kilometers
Copy link

kilometers commented Mar 20, 2022

+1 -- I'm getting this too.

@LucidThought
Copy link

+1 - I see this as well. I need to reconnect all rooms to the Redis adapter after error in the redis client connections, so this definition would be greatly helpful

@andrewda
Copy link

Although type assertions aren't ideal, something like this should work for now and is better than // @ts-ignore everywhere.

import { RedisAdapter } from '@socket.io/redis-adapter';

// ...

const rooms = await (io.of('/').adapter as RedisAdapter).allRooms();

The issue stems from socket.io using the Adapter type, which specific adapters then extend with their own implementations. Since there's no feedback about these new types to the socket.io module at build time, it has no knowledge that new types should be used. I haven't done enough digging to see if there's an issue open on the main repository about this, but one solution would be to add a getAdapter function like so:

getAdapter<T extends Adapter>(): T {
  return this.adapter as T;
}

Then, you would be able to call:

const rooms = await io.of('/').getAdapter<RedisAdapter>().allRooms();

I'm no TypeScript genie, so someone might have a better, more elegant solution to this problem!

@darrachequesne
Copy link
Member

You are right, the allRooms() method is deliberately not exposed, as it returns every rooms in the cluster, even private ones (the ones linked to each connection, based on the Socket#id attribute). The allRooms() method is specific to this adapter, and might be removed in the next major release.

You can use the serverSideEmit() method instead:

const fetchPublicRooms = (namespace) => {
  const rooms = [];

  namespace.adapter.rooms.forEach((sockets, room) => {
    const isPrivate = sockets.size === 1 && sockets.has(room);
    if (!isPrivate) {
      rooms.push(room);
    }
  });

  return rooms;
}

// requester
const allRooms = fetchPublicRooms(io.of("/"));

io.timeout(5000).serverSideEmit("all-rooms", (err, responses) => {
  if (err) {
    // something went wrong
  }
  responses.forEach(rooms => {
    Array.prototype.push.apply(allRooms, rooms):
  ));
  console.log(allRooms);
});

// receiver
io.on("all-rooms", (callback) => {
  callback(fetchPublicRooms(io.of("/")));
});

@darrachequesne
Copy link
Member

Closed due to inactivity, please reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants