title: Detect when user focuses on document
body: <p>I have a chat application for two persons and for example, one of them sends a message to the second one. </p>
  
  <p>When the second one delivers and focuses on document then the browser checks if last element in parent div has class "his_message" and if it does, the browser (via socket.io) sends the message to first user (who sent message) that his partner has seen his message, otherwise: do nothing. </p>
  
  <p>It is like Facebook's "seen" feature when Facebook notifies you that your chat partner has seen your message.</p>
  
  <p>I used MutationObserver for implementing what i want:</p>
  
  <pre><code>const observer = new MutationObserver((mutations)=&gt;{   
      if(document.hasFocus()){
      console.log($('#output &gt; :last-child').attr('class'));
      socket.emit('seen')
      }
   })
  
  observer.observe(output, { attributes: true, childList: true, characterData: true, subtree: true });
  </code></pre>
  
  <p>But if you test it, you will know that it is not working like I want. So what can i do to reach my aim? Thanks!</p>