`MaxListenersExceededWarning` on using `tailFile.on` inside `socket` connection

(node:4068) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 update listeners added to [TailFile]. Use emitter.setMaxListeners() to increase limit
new event listners (tailFile.on('update')) are created whenever new socket connection is established, causing the above warning. what could be better wat to handle this? TailFile class extends events.EventEmitter.
import { Server } from 'socket.io'
import TailFile from './tail-file'

const tailFile = new TailFile('server.log')
tailFile.init()

const io = new Server(8000, { cors: { origin: 'http://localhost:3000' } })

io.on('connection', (socket) => {
const queue = tailFile.get()
socket.emit('init', queue)

tailFile.on('update', (data) => socket.emit('update', data))
})
import { Server } from 'socket.io'
import TailFile from './tail-file'

const tailFile = new TailFile('server.log')
tailFile.init()

const io = new Server(8000, { cors: { origin: 'http://localhost:3000' } })

io.on('connection', (socket) => {
const queue = tailFile.get()
socket.emit('init', queue)

tailFile.on('update', (data) => socket.emit('update', data))
})
1 Reply
harshcut
harshcut9mo ago
some more information that might help: - i will only push update data to connections, and won't receive any from them - every new connection will be sent init data, and thereafter only update data
Want results from more Discord servers?
Add your server