title: Anyway to stop Apache server from reconnecting?
body: <p>I'm sure that due to the server disconnecting and reconnecting, my chat application is being affected. It is supposed to filter messages that are repeated and for the most part does this. However, when two (or more) users that are messaging each other are connected at the same time, their messages seem to repeat. This doesn't happen if only one user is viewing their messages. The application is using the Server-Side Events JavaScript API. </p>
  
  <p>Anyway, is there any way to keep the connection alive? Or a way to work around this? Possibly through server side message filtering?</p>
  
  <p>client-side:</p>
  
  <pre><code>function chat() {
      if (document.getElementById('chat').style.display == "none" || document.getElementById('chat').className != "on") {
          if (window.matchMedia("screen and (max-width: 800px)").matches) {
              document.getElementById('content').className = 'off';
              document.getElementById('chat').className = "on";
              if (document.getElementById('coverPhoto')) {document.getElementById('coverPhoto').className = 'off';}
          } else {
              document.getElementById('chat').style.display = 'block';
          }
          document.getElementById('chatBox').contentDocument.designMode = 'On';
          if (typeof(EventSource) !== "undefined") {
              var source = new EventSource('/ajax/gMessages.php'), last_id, used = {};
              source.addEventListener('message', function(event) {
                  var now_id = event.lastEventId;
                  if (last_id != now_id &amp;&amp; document.getElementById('chatMsgs').innerHTML != event.data) {
                      last_id = event.lastEventId;
                      if (!used[event.data]) {
                          document.getElementById('chatMsgs').innerHTML += event.data;
                          used[event.data] = 1;
                          document.getElementById('chat').scrollTop = document.getElementById('chat').scrollHeight; //keep scrollbar at the bottom
                      }
                  }   
              }, false);
          } 
      } else if (document.getElementById('chat').style.display == "block" || document.getElementById('chat').className != "off") {
          if (window.matchMedia("screen and (max-width: 800px)").matches) {
              document.getElementById('content').className = 'on';
              document.getElementById('chat').className = 'off';
              if (document.getElementById('coverPhoto')) {document.getElementById('coverPhoto').className = 'on';}
          } else {
              document.getElementById('chat').style.display = "block";
          }
      }
  }
  </code></pre>
  
  <p>server-side: </p>
  
  <pre><code>&lt;?php
  session_start();
  header('Content-Type: text/event-stream');
  header('Cache-Control: no-cache');
  set_time_limit(0);
  include "../includes/config.php"; 
  include "../includes/date.php";
  function getMessages() {
      $con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);   
      $con-&gt;exec("SET CHARACTER SET utf8"); 
      $getMsgs = "SELECT * FROM `messages` WHERE `msg_to` = '$_SESSION[username]' AND `type` = '0' ORDER BY `sent` ASC";
      $receivedMsgs = $con-&gt;query($getMsgs); 
      foreach ($receivedMsgs-&gt;fetchAll() as $msg) { 
          $getUI = "SELECT `profile_pic`, `full_name`, `username` FROM `users` WHERE `username` = '$msg[msg_from]'";
          $uiQuery = $con-&gt;query($getUI);
          while ($UI = $uiQuery-&gt;fetch(PDO::FETCH_ASSOC)) { 
              echo "id: {$msg['id']}\n"; 
              echo "data: &lt;article&gt;\n"; 
              echo "data: &lt;img src='{$UI[profile_pic]}' alt='{$UI[full_name]}' /&gt;\n"; 
              echo "data: &lt;section class='msgCntnt'&gt;\n";
              echo "data: &lt;p class='timeAgo'&gt;". Agotime($msg[sent]) ."&lt;/p&gt;\n"; 
              echo "data: &lt;a href='/profile?u={$UI[username]}'&gt;{$UI['full_name']}&lt;/a&gt;&lt;br /&gt;\n"; 
              echo "data: &lt;h4&gt;@{$msg['msg_from']}&lt;/h4&gt;\n"; 
              echo "data: &lt;p&gt;{$msg['message']}&lt;/p&gt;\n"; 
              echo "data: &lt;/section&gt;\n"; 
              echo "data: &lt;/article&gt;\n\n"; 
          } 
              ob_flush();
              flush();
              sleep(1);
      }
  }
  getMessages();
  ?&gt;
  </code></pre>