Answer the question
In order to leave comments, you need to log in
How to remove a specific socket.io listener?
Hello, there is such a function
exports.list = () => {
return new Promise((resolve, reject) => {
try {
if (conect) {
const hash = crypto.randomBytes(30).toString('hex')
socket
.emit('list', JSON.stringify({
auth: key,
hash: hash
}))
.on('list', (data) => {
data = JSON.parse(data)
if (data.hash === hash) {
resolve(data.list)
}
});
} else {
reject(new Error('Disconnect to socket API'))
}
} catch (error) {
reject(error)
}
})
}
Answer the question
In order to leave comments, you need to log in
There are two options:
1. Put the listener globally and don't remove it. (More correct)
2. Use instead of .on(), .once(). It creates a listener and after one event it will be removed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question