MaxListenersExceededWarning:检测到可能的EventEmitter内存泄漏。添加了11条消息lis teners。使用emitter.setMaxListeners()来增加限制 [英] MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message lis teners added. Use emitter.setMaxListeners() to increase limit

查看:200
本文介绍了MaxListenersExceededWarning:检测到可能的EventEmitter内存泄漏。添加了11条消息lis teners。使用emitter.setMaxListeners()来增加限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能会标记为重复的解决方案,但堆栈溢出的解决方案对我不起作用。

I know this might flag as a duplicate solution but solution on stack overflow is not working for me.

问题:

(node:5716) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message lis
teners added. Use emitter.setMaxListeners() to increase limit.

我的代码库很大,我有时会遇到此错误,我不知道为什么会发生这种情况

My code base is huge and i facing this error sometimes i don't know why it is happening

我尝试了什么:

试图增加听众限制,但不幸的是它没有用。

Tried to increase the listeners limit but unfortunately it is not working.

const EventEmitter = require('events');
const emitter = new EventEmitter()
emitter.setMaxListeners(50)

更新:

在浏览一些网页后,我运行此命令来跟踪漫游

After Some browsing I run this command to trace wanrning

node --trace-warnings index.babel.js

结果是我的socket.io代码是我使用socket.io与redis的问题

Turns out be my socket.io code is the problem I am using socket.io with redis

这是错误

node:14212) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message li
steners added. Use emitter.setMaxListeners() to increase limit
    at _addListener (events.js:281:19)
    at RedisClient.addListener (events.js:298:10)
    at Namespace.<anonymous> (D:/newProject/services/socket.js:21:17)
    at emitOne (events.js:115:13)
    at Namespace.emit (events.js:210:7)
    at Namespace.emit (D:\newProject\node_modules\socket.io\lib\namespace.js:213:10)
    at D:\newProject\node_modules\socket.io\lib\namespace.js:181:14
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)

这是代码(但是这段代码用于更具体的任务,它不会全部执行时间)

this is the code (But this code is for more specific tasks it will not execute all the time)

const redis = require('redis');
const config = require('../config')
const sub = redis.createClient(config.REDIS.port, config.REDIS.host);
const pub = redis.createClient(config.REDIS.port, config.REDIS.host);

sub.subscribe('spread');


module.exports = io => {
    io.on('connection',(socket) => {

        let passport  = socket.handshake.session.passport;  /* To find the User Login  */
        if(typeof passport !== "undefined") {


            socket.on('typing:send',(data) => {

                pub.publish('spread',JSON.stringify(data))
            });
            sub.on('message',(ch,msg) => { // this is the Exact line where I am getting this error


                io.emit(`${JSON.parse(msg).commonID}:receive`,{...JSON.parse(msg)})
            })


        }
    });
};


推荐答案

事件发射器是10.您可以使用emitter.setMaxListeners增加它。我的建议是不要改变它,除非并且直到明确要求,听众因为你没有取消订阅而增加。现在你的代码。

Default limit for Event Emitter is 10. You can increase it with emitter.setMaxListeners. My suggestion is not to change it unless and until explicitly required, listeners are increased because you didn't unsubscribed. Now to your code.

const redis = require('redis');
const config = require('../config')
const sub = redis.createClient(config.REDIS.port, config.REDIS.host);
const pub = redis.createClient(config.REDIS.port, config.REDIS.host);

sub.subscribe('spread');


module.exports = io => {
    io.on('connection',(socket) => {
    //COMMENT : This callback will be executed for all the socket connections. 
        let passport  = socket.handshake.session.passport;  /* To find the User Login  */
        if(typeof passport !== "undefined") {


            socket.on('typing:send',(data) => {

                pub.publish('spread',JSON.stringify(data))
            });
            // COMMENT : This is where you are subscribing for each and every socket conected to your server
            sub.on('message',(ch,msg) => { // this is the Exact line where I am getting this error


//COMMENT : Where as you are emiting message on socket manager not on socket. 
io.emit(`${JSON.parse(msg).commonID}:receive`,{...JSON.parse(msg)})
            })


        }
    });
};

现在,如果我们分析上面的代码,那么如果你打开20插槽连接到您的服务器它将订阅20次,这里出错。
现在,如果您的要求是在服务器级别监听redis上发布的消息然后在io上发出,那么您的代码应该如下所示

Now if we analyse above code then if you open 20 socket connection to your server it will subscribe for 20 time, here it is going wrong. Now if your requirement is to listen for the message published on redis at server level and then emit on io then your code should be like below

const redis = require('redis');
const config = require('../config')
const sub = redis.createClient(config.REDIS.port, config.REDIS.host);
const pub = redis.createClient(config.REDIS.port, config.REDIS.host);

sub.subscribe('spread');


module.exports = io => {
sub.on('message',(ch,msg) => { // this is the Exact line where I am getting this error


                io.emit(`${JSON.parse(msg).commonID}:receive`,{...JSON.parse(msg)});
        });
    io.on('connection',(socket) => {

        let passport  = socket.handshake.session.passport;  /* To find the User Login  */
        if(typeof passport !== "undefined") {


            socket.on('typing:send',(data) => {

                pub.publish('spread',JSON.stringify(data))
            });
            


        }
    });
};

这篇关于MaxListenersExceededWarning:检测到可能的EventEmitter内存泄漏。添加了11条消息lis teners。使用emitter.setMaxListeners()来增加限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆