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

Any reason emit events on namespace level will be broadcasting instead of only to subscribers like on top level #155

Open
wwth opened this issue Dec 20, 2023 · 1 comment

Comments

@wwth
Copy link

wwth commented Dec 20, 2023

This is the code for emit on the top level (not namespace) with considerations of subscribers

        // forward emitted event to subscribers
        this.on(name, (...params) =>
        {
            // flatten an object if no spreading is wanted
            if (params.length === 1 && params[0] instanceof Object)
                params = params[0]

            for (const socket_id of this.namespaces[ns].events[name].sockets)
            {
                const socket = this.namespaces[ns].clients.get(socket_id)

                if (!socket)
                    continue

                socket.send(this.dataPack.encode({
                    notification: name,
                    params
                }))
            }
        })

This emit didn't consider subscribers of the events, and will broadcast to the namespace clients regardless subscribed or not

/**
             * Emits a specified event to this namespace.
             * @inner
             * @method
             * @param {String} event - event name
             * @param {Array} params - event parameters
             * @return {Undefined}
             */
            emit(event: string, ...params: Array<string>)
            {
                const socket_ids = [ ...self.namespaces[name].clients.keys() ]

                for (let i = 0, id; id = socket_ids[i]; ++i)
                {
                    self.namespaces[name].clients.get(id).send(self.dataPack.encode({
                        notification: event,
                        params: params || []
                    }))
                }
            },
@mkozjak
Copy link
Member

mkozjak commented Apr 18, 2024

Definitely shouldn't be like that. PR welcome!

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

2 participants