事件发射器的默认限制为10。您可以使用generator.setMaxListeners增加它。我的建议是不要更改它,除非并且直到明确要求为止,因为您没有取消订阅,所以增加了侦听器。现在到您的代码。
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上发出,则您的代码应如下所示
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 errorio.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)) });} });};解决方法
我知道这可能标记为重复的解决方案,但是堆栈溢出的解决方案对我来说不起作用。
问题:
(node:5716) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added. Use emitter.setMaxListeners() to increase limit.
我的代码库很大,有时我会遇到此错误,我不知道为什么会发生
我试过了
试图增加听众的限制,但不幸的是它没有用。
const EventEmitter = require(’events’);const emitter = new EventEmitter()emitter.setMaxListeners(50)
更新:
经过一番浏览后,我运行此命令来跟踪wanrning
node --trace-warnings index.babel.js
原来是我的socket.io代码是我正在使用带redis的socket.io的问题
这是错误
node:14212) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners 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:newProjectnode_modulessocket.iolibnamespace.js:213:10) at D:newProjectnode_modulessocket.iolibnamespace.js:181:14 at _combinedTickCallback (internal/process/next_tick.js:131:7) at process._tickCallback (internal/process/next_tick.js:180:9)
这是代码(但是此代码用于更具体的任务,它不会一直执行)
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 errorio.emit(`${JSON.parse(msg).commonID}:receive`,{...JSON.parse(msg)}) })} });};