How to properly remove event listeners in node js eventemitter

event-stream, eventemitter, express, javascript, node.js

Solution

You should name the function you attach as event handler. Then on removing it, you just pass the function by name:

app.get('/api/:boardname/remoterefresh', function(req, res){
    var boardname = req.param('boardname')
    function refreshHandler(data){
        setTimeout(function(){
            res.write('data: '+data+'\n\n');
        }, 1000)
    }
    rr.on("refresh-"+boardname, refreshHandler);

    req.socket.setTimeout(Infinity);

    res.writeHead(200, {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive'
    });

    res.write('\n');

    req.on('close', function(){
        console.log('closed')
        rr.removeListener("refresh-"+boardname, refreshHandler);
    });
});

Basically `removeListener` will look up the given function by reference, if it found that function it will remove it from the event hander.

Problem

I have a set up with a GET and a POST route, the idea being that POSTing to the route triggers an event, and the GET route is a server-sent eventstream which fires each time the POSTed event is triggered... however, i think i've done something wrong as event listeners seem to get added routinely despite only having one event stream subscriber... what am i doing wrong? ``` var events = require('events'), EventEmitter = events.EventEmitter, rr = new EventEmitter(); app.post('/api/:boardname/remoterefresh', function(req, res){ var boardname = req.param('boardname'), data = new Date().getTime(); rr.emit("refresh-"+boardname, data) res.json({data: data}) }); app.get('/api/:boardname/remoterefresh', function(req, res){ var boardname = req.param('boardname') rr.on("refresh-"+boardname, function(data){ setTimeout(function(){ res.write('data: '+data+'\n\n'); }, 1000) }); req.socket.setTimeout(Infinity); res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }); res.write('\n'); req.on('close', function(){ console.log('closed') rr.removeListener("refresh-"+boardname, function(){ // }) }) }) ```

Original source

Related problems