Socket.io Chat Application Shows EventEmitter memory leak detection. 11 listeners added. Use emitter.setMaxListeners ()

I use socket.io and node.js plus mysql for a private chat application and when I use socket.on('example', function(data){...}); Here it gives such an error code.

  node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. Trace at PoolConnection.EventEmitter.addListener (events.js:175:15) at io.on.eventConnection (/Applications/MAMP/htdocs/mysite/node/server.js:72:15) at Ping.onOperationComplete [as _callback] (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/Pool.js:99:5) at Ping.Sequence.end (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24) at Ping.Sequence.OkPacket (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:105:8) at Protocol._parsePacket (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/protocol/Protocol.js:280:23) at Parser.write (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/protocol/Parser.js:73:12) at Protocol.write (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/protocol/Protocol.js:39:16) at Socket.Connection.connect (/Applications/MAMP/htdocs/mysite/node/node_modules/mysql/lib/Connection.js:96:28) at Socket.EventEmitter.emit (events.js:96:17) 

And my code in the server side: -

 function fetchMessages(data){ var sql = "SELECT `msg_descr`,`msg_to` FROM `message` WHERE (msg_to="+data[0].iChatUserId+" AND msg_from="+data[1]+") OR (msg_to="+data[1]+" AND msg_from="+data[0].iChatUserId+") ORDER BY msg_created_date DESC LIMIT 20"; eventConnection(sql, function(callback, rows){ if(callback){ users[data[1]].emit('Release Msg', {messages:rows, selfId:data[0].iChatUserId}); } }); } //when a user is actibe socket.on('load Message', function(data, callback){ var repsondMsg = fetchMessages(data); }); 

On the client side: -

  chatList.click(function(){ var __this = $(this); targetData = []; __userData = fetchUserData(__this); myId = myImage.data('myid'); targetData.push(__userData, myId); socket.emit('load Message', targetData); checkExistance(targetData); }); socket.on('Release Msg', function(data){ $.each(data.messages, function(){ $.each(this, function(k,v){ $('*[data-ichatid="'+data.selfId+'"]').children('.body_cht_box_ind').children('#chats').append($('<li>').text(v)); }) }); }); 
+5
source share
1 answer

The following link has an explanation. https://nodejs.org/docs/latest/api/events.html#events_emitter_setmaxlisteners_n

By default, EventEmitters will issue a warning if more than 10 listeners are added for a particular event. This is a useful default that helps you find memory leaks. Obviously, not all events should be limited to only 10 listeners. The emitter.setMaxListeners () method allows you to change the limit for this particular instance of EventEmitter. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners. "

you can use something like this.

 require('events').EventEmitter.defaultMaxListeners = Infinity; 
0
source

Source: https://habr.com/ru/post/1241716/


All Articles