Welcome.
This will have least requirements.
Note: SERIAL
is: BIGINT UNSIGNED UNIQUE AUTO_INCREMENT
Table channel
has following fields:
id
SERIAL, not primaryname
VARCHAR(255)
Optional fields:
logo_blob
BLOBlogo_url
VARCHAR(2048)
Table messages
has following fields:
id
SERIAL, not primaryuid
BIGINT, attribute:UNSIGNED
message
TEXT, or VARCHARchannel_id
BIGINT UNIQUE, attribute:UNSIGNED
Table last_message
This table does not spam with INSERT
, it's done only once,
Meaning: every time new message is sent, UPDATE
must be called immediately.
Has following fields:
last_id
BIGINT, attribute:UNSIGNED
channel_id
BIGINT UNIQUE, attribute:UNSIGNED
uid
BIGINT, attribute:UNSIGNED
The channel_id
will crash INSERT
if duplicate id is found.
Thus UPDATE
must be called instead.
Assuming that php will be used, you would want to use usleep(256e3)
for the MINIMUM delay between each fetchall
.
However if you are sure your server is capable of higher stress, use usleep(128e3)
instead.
For free hosting, about usleep(500e3)
is recommended.
This is how you do realtime fetch:
// dummy functions, let's assume you wrote actual code
function fetchAll($last_msg){
$last_id = $last_msg->last_id;
$q = "SELECT * from messages where id>$last_id";
//...
}
function get_last_message($channel_id){
$q = "SELECT * from last_message where channel_id='$channel_id'";
//...
}
function sendmessage(){
// or use INSERT, if not inserted!
$q = "UPDATE last_message set ...";
// you know what to do here:
$q2 = "INSERT into messages set ...";
// ...
}
$channel_id = 1;
$maxticks = 32;
$last_msg = get_last_message($channel_id);
// last_msg contains last_id which is later used by fetchAll to fecth messages AFTER that id
while ($maxticks--) {
$curr_msg = get_last_message($channel_id);
if ($last_msg->last_id != $curr_msg->last_id) {
$last_msg = $curr_msg;
break;
}
usleep(500e3);
}
echo fetchAll($last_msg);