Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

392
Views
Websocket memory leak node.js. Event emitters?

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);
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!