title: Interaction between expressjs, socket.io and EventEmitter listeners
body: <p>I have a server setup using expressjs which communicates with the front end clients using socket.io. The server runs on the raspberry pi and provides a backend to control mplayer player for my music library using the mplayer nodejs module. </p>
  
  <p>Most of the actions to update the clients on the front end are taken based on events emitted by the mplayer EventEmitter module. For example :</p>
  
  <p>When a command to stop the player is issued as <code>player.stop()</code> the EventEmitter instance of the player emits <code>'stop'</code> event.</p>
  
  <p>The server then listens for this event as follows :
  <code>player.on('stop', socket.emit('stop'));</code></p>
  
  <p>The issue that I have noticed is as more clients connect to the server over socket io each client creates a listener for the mplayer EventEmitter <code>'stop'</code> event !! So for example if there were 3 clients, I would tend to receive 3 <code>'stop'</code> events from the player EventEmitter and consequently send 3 <code>'stop'</code> events to my front end clients.</p>
  
  <p>My question is how can I ensure that only one listener is created for the player event? Why does every client bind itself as a listener to the EventEmitter event ?</p>
  
  <p>My server side socketio setup looks as follows :</p>
  
  <pre><code>io.on('connection', function(socket) {
      console.log('a user connected @ ' + socket.id);
  
      ///// Listen for player to successfully stop playing or when EOF is reached
      player.on('stop', function() {
          console.log("Received Stop Event From Player");
          socket.emit('song:next'); //Play next song
      }
  </code></pre>
  
  <p>Console output when 3 clients are connected, on receiving the <code>'stop'</code> event :</p>
  
  <pre><code>Received Stop Event From Player
  Received Stop Event From Player
  Received Stop Event From Player
  </code></pre>
  
  <p>This leads me to send 3 <code>'next'</code> events to be send to all my clients. </p>
  
  <p>MPlayer nodejs looks like this :</p>
  
  <pre><code>this.player.on('playstop', function() {
      if(options.verbose) {
          console.log('player.stop');
      }
      this.emit('stop')
  }.bind(this));
  </code></pre>
  
  <p><a href="https://github.com/noodny/mplayer" rel="nofollow noreferrer">mplayer for nodejs</a></p>
  
  <p>On further investigation through stackoverflow it seems similar to this question :
  <a href="https://stackoverflow.com/questions/23209072/local-eventemitter-listening-to-global-eventemitter?rq=1">Local eventEmitter listening to global eventEmitter</a></p>
  
  <p>But the solution provided in the comments doesn't make sense to me because of the use of addListener and removeListener ?? Can someone break it down for me and explain how I would achieve what is mentioned there ?</p>
  
  <p><a href="https://i.stack.imgur.com/BROxi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BROxi.png" alt="This is a diagram of what I understand is happening. Any way to circumvent this ???"></a></p>