Trying to build a chat system using html5 server sent events

AlexTCGPro

Member
Hey guys.
I'm trying to develop a simple chat system for my website, it has to handle about 500 users, unfortunately my host server doesn't allow the use of websockets so I'm stuck with the next best option, which seems to be server sent events. I'm trying to build it directly through joomla and fabrik lists but there's very little documentation about this. Could you give me some guidance?

I have a list like so:
Code:
chat (list)
-id (id)
-sender (userid)
-receiver (databasejoin -> userid)
-message (textarea)
-datetime (datetime)
The idea is to basically have a PHP script that keeps a connection with the server and echoes new messages whenever they come. I understand is not instantly but it's good enough and what some companies still use nowadays.

I came across this code (from here https://stackoverflow.com/questions/45568180/server-side-events-with-php-mysql)
Code:
<?php
include 'conn.php'; // database connection

header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");

$query = "SELECT TimeStamp, CardNo FROM transactions WHERE TimeStamp > ?";
$stmt = $conn->prepare($query);
$ts = time();

while(true)
{
    if ($result = $stmt->execute([$ts])) {
        $row = $result->fetch_assoc();
        echo "data: " . $row['CardNo'] . "\n\n";
        $ts = $row['TimeStamp'];
        flush();
    }
    sleep(2);
}
Seems extremely simple, but there's no guarantee it works, I tried to tweak it a little to accommodate for joomla's own database system but so far had no results, I'm going the right way?
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top