Too many clients connected to node js chat app

Viewed 26

Im working on a chat application using node.js. I'm coming up with an error though.

(node:72307) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 connection listeners added to [Namespace]. Use emitter.setMaxListeners() to increase limit

Now, I'm assuming that this is because my clients are not disconnecting from my server, because it wasnt doing this. I connected about 10 times before it started doing this and as I heard, the default amount is 10. I cant really find in my code though what is the issue as to why they are not disconnecting. Any help would be greatly appriciated.

var app = require("express")();
var http = require("http");
var io = require("socket.io")(http);
var fs = require("fs");
http
  .createServer(function (req, res) {
    fs.readFile("index.html", function (err, data) {
      res.writeHead(200, { "Content-Type": "text/html" });
      res.write(data);
      return res.end();
    });

    io.once("connection", function (socket) {
      console.log("ALERT!!:: A USER HAS CONNECTED!!");
      socket.once("disconnect", function () {
        console.log("user discconected...");
      });
    });
    io.once("connection", function (socket) {
      socket.on("chat message", function (msg) {
        console.log("message: " + msg);
        io.once("connection", function (socket) {
          socket.broadcast.emit("hi");
        });
        io.once("connection", function (socket) {
          socket.on("chat message", function (msg) {
            io.emit("chat message", msg);
          });
        });
      });
    });
  })
  .listen(2000);
0 Answers
Related