Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to filter SSE script for duplicate messages?
    primarykey
    data
    text
    <p>There seems to be a strange bug within a Server-sent Event implementation I've created. The problem seems to lie within on the client-side through a filtering system. The issue is that the script is supposed to only update the application data stream when there is new info, instead of just continuously repeating everything. Currently, the script stops the stream from repeating everything but the last message received. This last message is the only one to repeat indefinitely. </p> <p>I tried to filter the messages on the server side as well but it seems to no avail. Any info I left out, just ask and I'd be more than happy to supply you with it.</p> <p>Client-side Javascript</p> <pre><code>function getMsgs() { if (typeof(EventSource) !== "undefined") { var source = new EventSource(''); var last_id; var used = {}; source.addEventListener('message', function(event) { if (event.origin != 'http://example.com') { alert('Error: Unidentified origin!'); return; } else { var now_id = event.lastEventId; if (window.matchMedia("screen and (max-width: 800px)").matches) { if (last_id != now_id &amp;&amp; document.getElementById('content').innerHTML != event.data) { last_id = event.lastEventId; if (!used[event.data]) { document.getElementById('content').innerHTML += event.data; used[event.data] = 1; } } } else { 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); } </code></pre> <p>Server side PHP</p> <p>*The <code>lstMsg</code> session is previously set when the user logs in</p> <pre><code>&lt;?php session_start(); header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); include "../includes/config.php"; include "../includes/functions.php"; function getMessages($time) { $con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); $con-&gt;exec("SET CHARACTER SET utf8"); $getLastMsg = "SELECT MAX(`sent`) FROM `messages` WHERE `msg_to` = '$_SESSION[username]' AND `type` = '0' LIMIT 1"; $retrieveTime = $con-&gt;query($getLastMsg); $_SESSION['lstMsg'] = $retrieveTime-&gt;fetchColumn(PDO::FETCH_ASSOC); $getMsgs = "SELECT * FROM `messages` WHERE `msg_to` = '$_SESSION[username]' AND `type` = '0' AND `sent` &gt; '$time' 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 class='post'&gt;\n"; echo "data: &lt;img src='{$UI[profile_pic]}' alt='{$UI[full_name]}' /&gt;\n"; echo "data: &lt;section class='pContainer'&gt;\n"; echo "data: &lt;p class='timeAgo'&gt;". Agotime($msg[sent]) ."&lt;/p&gt;\n"; echo "data: &lt;a style='position:absolute;' href='/profile?uid={$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 class='postContent'&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($_SESSION['lstMsg']); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload