Skip to content
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

this.children is undefined for ParentBroadcastAdapter broadcast #4985

Closed
charliegracie opened this issue Mar 27, 2024 · 6 comments · Fixed by #5009
Closed

this.children is undefined for ParentBroadcastAdapter broadcast #4985

charliegracie opened this issue Mar 27, 2024 · 6 comments · Fixed by #5009

Comments

@charliegracie
Copy link

I am getting an error in my application after moving up to version 4.7.5. When I run with previous versions, including 4.7.4, everything works properly. The error I receive is:

node_modules/socket.io/dist/parent-namespace.js:88
        this.children.forEach((nsp) => {
                      ^

TypeError: Cannot read properties of undefined (reading 'forEach')
    at ParentBroadcastAdapter.broadcast (node_modules/socket.io/dist/parent-namespace.js:88:23)
    at BroadcastOperator.emit (node_modules/socket.io/dist/broadcast-operator.js:169:26)
    at Socket.<anonymous> (server.js:60:33)
    at Socket.emit (node:events:520:28)
    at Socket.emitReserved (node_modules/socket.io/dist/typed-events.js:56:22)
    at Socket._onclose (node_modules/socket.io/dist/socket.js:547:14)
    at Client.onclose (node_modules/socket.io/dist/client.js:247:20)
    at Socket.emit (node:events:532:35)
    at Socket.onClose (node_modules/engine.io/build/socket.js:304:18)
    at Object.onceWrapper (node:events:639:28)

When the following code was added in PR 4950 it appears that the code forgot to store or copy the children parameter.

this.children.forEach((nsp) => {

I think the code should be modified something like:

class ParentBroadcastAdapter extends Adapter {
  private readonly children: Set<
    Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
  > ;
  constructor(parentNsp: any, private readonly children: Set<Namespace>) {
    super(parentNsp);

     this.children = children;
  }

  broadcast(packet: any, opts: BroadcastOptions) {
    this.children.forEach((nsp) => {
      nsp.adapter.broadcast(packet, opts);
    });
  }
}
@darrachequesne
Copy link
Member

Hi! That's weird, the children attribute should be initialized with the readonly tag:

constructor(parentNsp: any, private readonly children: Set<Namespace>) {
super(parentNsp);
}

In dist/parent-namespace.js:

class ParentBroadcastAdapter extends socket_io_adapter_1.Adapter {
    constructor(parentNsp, children) {
        super(parentNsp);
        this.children = children;
    }
    broadcast(packet, opts) {
        this.children.forEach((nsp) => {
            nsp.adapter.broadcast(packet, opts);
        });
    }
}

So I'm not sure how this.children can be undefined there... Thoughts?

@peterjuras
Copy link

Hi,
I'm experiencing the same issue. I'm using https://github.com/socketio/socket.io-redis-streams-adapter maybe that is the source of the issue?

@gkt5015
Copy link
Contributor

gkt5015 commented Apr 24, 2024

Hi, is there an update on this?

I believe the issue here is that this.children isn't declared yet when super is called in the ParentNamespace class. The constructor of Namespace will call _initAdapter of the subclass with the undefined value. Prior to the change, this.children was enclosed in the function so it wasn't an issue.

Perhaps one way to change this would be:

class ParentBroadcastAdapter extends socket_io_adapter_1.Adapter {
    constructor(parentNsp) {
        super(parentNsp);
        this.parentNsp = parentNsp;
    }
    broadcast(packet, opts) {
        this.parentNsp.children.forEach((nsp) => {
            nsp.adapter.broadcast(packet, opts);
        });
    }
}

darrachequesne pushed a commit that referenced this issue Apr 26, 2024
Following [1], emitting from a dynamic namespace to a room would throw
this error:

> node_modules/socket.io/dist/parent-namespace.js:88
>         this.children.forEach((nsp) => {
>                       ^
> 
> TypeError: Cannot read properties of undefined (reading 'forEach')
>     at ParentBroadcastAdapter.broadcast (node_modules/socket.io/dist/parent-namespace.js:88:23)
>    at BroadcastOperator.emit (node_modules/socket.io/dist/broadcast-operator.js:169:26)
>    at Socket.<anonymous> (server.js:60:33)
>    at Socket.emit (node:events:520:28)
>    at Socket.emitReserved (node_modules/socket.io/dist/typed-events.js:56:22)
>    at Socket._onclose (node_modules/socket.io/dist/socket.js:547:14)
>    at Client.onclose (node_modules/socket.io/dist/client.js:247:20)
>    at Socket.emit (node:events:532:35)
>    at Socket.onClose (node_modules/engine.io/build/socket.js:304:18)
>    at Object.onceWrapper (node:events:639:28)

Previous output code:

```js
class ParentNamespace extends namespace_1.Namespace {
    constructor(server) {
        super(server, "/_" + ParentNamespace.count++);
        this.children = new Set();
    }
    _initAdapter() {
        this.adapter = new ParentBroadcastAdapter(this, this.children);
    }
}
```

Here, `super()` calls `Namespace._initAdapter()`, but `this.children`
is not defined yet, hence the problem.

[1]: b9ce6a2

Related: #4985
@Hippopop
Copy link

Hi! I am using the latest version of the package. And I am facing this exact same issue!
I'm not sure why that's happening and how to resolve it?!?

/Volumes/Development/NodeJs/Personal/full-stack-chat-app/chat-server/node_modules/socket.io/dist/parent-namespace.js:88
        this.children.forEach((nsp) => {
                      ^
TypeError: Cannot read properties of undefined (reading 'forEach')
    at ParentBroadcastAdapter.broadcast (/Volumes/Development/NodeJs/Personal/full-stack-chat-app/chat-server/node_modules/socket.io/dist/parent-namespace.js:88:23)
    at BroadcastOperator.emit (/Volumes/Development/NodeJs/Personal/full-stack-chat-app/chat-server/node_modules/socket.io/dist/broadcast-operator.js:169:26)
    at Namespace.<anonymous> (/Volumes/Development/NodeJs/Personal/full-stack-chat-app/chat-server/src/socket_io/chat_process/chat_process.ts:35:14)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

@darrachequesne
Copy link
Member

@Hippopop I don't think the fix has been released yet. Let me check.

@Hippopop
Copy link

@darrachequesne Yeah, I think that's the case. But I've been trying to to use the package/library directly from the GitHub. And use the #main brunch. Sadly that's not working either.

npm error Cannot read properties of undefined (reading 'spec')
npm error A complete log of this run can be found in: ...

Is there anything I can do here?!? I'm a bit new with Node.Js and npm.

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

Successfully merging a pull request may close this issue.

5 participants