0

I am getting the event emitter leak after using my code 10 times essentially. I understand the default of event emitter auto sending out a warning in the console. My question is what in this code is directly creating the event listeners? Is it poor coding on my part or is it just how the websockets are stacked onto each other?

I'll explain the code a bit. I have one websocket within another and I figured it would serve the data to a web page essentially flowing from Twitch to a localhost site. However, if I use the keywords more than 10 times, I get the error. I do not understand enough about WebSockets to really understand why my code creates a new listener with each msg.text received so anyone with a bit more understanding please help!

I believe me issue to be similar to this though I am having a hard time conceptualizing my own code here

const { paintballShot } = require('./JavaScript/paintballGunFire');
const { readPin } = require('./JavaScript/readPin');
const ws = require('ws');
const express = require('express');


const app = express();

//CONNECT TO TWITCH
let client = new ChatClient({
  connection: {
    type: "websocket",
    secure: true,
  }
});

//connected?
client.on("ready", () => console.log("Successfully connected to chat"));
client.on("close", (error) => {
  if (error != null) {
    console.error("Client closed due to error", error);
  }
});

//create headless websocket
const wsServer = new ws.Server({ noServer: true });
wsServer.on('connection', function connection(socket) {

//call other websocket connected to Twitch from inside the new websocket
client.on("PRIVMSG", (msg, error) => {
  if (msg.messageText === "right") {
    socket.send(JSON.stringify(`${msg.displayName}: ${msg.messageText}`));
  }
  
  if (msg.messageText === "left") {
    socket.send(JSON.stringify(`${msg.displayName}: ${msg.messageText}`));
  }

  if (msg.messageText === "fire") {
    socket.send(JSON.stringify(`${msg.displayName}: ${msg.messageText}`));
    paintballShot();
  }

  if (msg.messageText === "pin") {
    readPin();
  }
  process.on('uncaughtException', function (err) {
    console.log(err);
}); 
});


client.connect();
client.join("channel");

  socket.on('message', message => console.log(message));
});

// `server` is a vanilla Node.js HTTP server
const server = app.listen(3000);
server.on('upgrade', (request, socket, head) => {
  wsServer.handleUpgrade(request, socket, head, socket => {
    wsServer.emit('connection', socket, request);
  });
});
process.on('uncaughtException', function (err) {
    console.log(err);
});
J. Doe
  • 43
  • 10
  • 1
    Every time you get a socket connection you do -> `client.on("PRIVMSG",` these are stacking and will never be removed, you would want to also do `client.off(` when connection is closed. You also have `process.on('uncaughtException'` inside there too, and that makes no sense. – Keith Nov 01 '21 at 16:52
  • client.off isn't an option I don't think? The idea is I want to constantly be listening and then trigger events each time a word is used. If I turn the websocket off, I can't listen right? Sorry about process.on. I was trying to understand what was happening. You think wrapping the second WebSocket in an if statement to turn it on and off could work? Just thinking out loud. – J. Doe Nov 01 '21 at 17:04
  • 1
    `If I turn the websocket off` I'm not telling you to turn the websocket off. Also I've no idea what methods `ChatClient` has, but if it doesn't have an off then I wouldn't use it in a connection event, otherwise you have no way of removing the event listener, and that's were you will get a memory leak. – Keith Nov 01 '21 at 17:10
  • 1
    Appreciate the help. – J. Doe Nov 01 '21 at 17:14

1 Answers1

0

To wrap this up, the library I am using (Dank TwitchIRC) does have a connection rate limiter that seems to work if you add it to your chat client in the beginning. If I set it low enough, depending on the messages received from Twitch, it will end connections just as fast, meaning no memory leak.

J. Doe
  • 43
  • 10