![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/public_html/radio/api/live/ |
<?php
/**
* Server-Sent Events (SSE) endpoint for real-time stream updates
*/
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('X-Accel-Buffering: no'); // Disable nginx buffering
require_once __DIR__ . '/../../../config/database.php';
$stream_id = isset($_GET['stream_id']) ? (int)$_GET['stream_id'] : 0;
if (!$stream_id) {
echo "data: " . json_encode(['error' => 'Invalid stream_id']) . "\n\n";
flush();
exit;
}
$pdo = getDBConnection();
// Prevent script timeout
set_time_limit(0);
ignore_user_abort(false);
$last_check = time();
$last_listener_count = 0;
$last_track_id = 0;
while (true) {
// Check if client disconnected
if (connection_aborted()) {
break;
}
// Get current stream status
$stmt = $pdo->prepare("
SELECT is_live, listener_count, current_track_id
FROM radio_streams
WHERE id = ?
");
$stmt->execute([$stream_id]);
$stream = $stmt->fetch();
if (!$stream || !$stream['is_live']) {
echo "data: " . json_encode(['type' => 'stream_offline']) . "\n\n";
flush();
break;
}
// Send listener count update if changed
if ($stream['listener_count'] != $last_listener_count) {
echo "data: " . json_encode([
'type' => 'listener_count',
'count' => $stream['listener_count']
]) . "\n\n";
flush();
$last_listener_count = $stream['listener_count'];
}
// Send now playing update if track changed
if ($stream['current_track_id'] != $last_track_id) {
if ($stream['current_track_id']) {
$stmt = $pdo->prepare("
SELECT np.*, mt.title, mt.audio_url, mt.image_url,
u.name as artist_name
FROM radio_now_playing np
JOIN music_tracks mt ON np.track_id = mt.id
LEFT JOIN users u ON mt.user_id = u.id
WHERE np.stream_id = ? AND np.track_id = ? AND np.ended_at IS NULL
ORDER BY np.started_at DESC
LIMIT 1
");
$stmt->execute([$stream_id, $stream['current_track_id']]);
$now_playing = $stmt->fetch();
if ($now_playing) {
echo "data: " . json_encode([
'type' => 'now_playing',
'track' => [
'id' => $now_playing['track_id'],
'title' => $now_playing['title'],
'artist' => $now_playing['artist_name'],
'audio_url' => $now_playing['audio_url'],
'image_url' => $now_playing['image_url'],
'started_at' => $now_playing['started_at']
]
]) . "\n\n";
flush();
}
}
$last_track_id = $stream['current_track_id'];
}
// Send vote updates every 5 seconds
if (time() - $last_check >= 5) {
$stmt = $pdo->prepare("
SELECT track_id, COUNT(*) as vote_count
FROM radio_votes
WHERE stream_id = ? AND voted_at > DATE_SUB(NOW(), INTERVAL 1 MINUTE)
GROUP BY track_id
");
$stmt->execute([$stream_id]);
$votes = $stmt->fetchAll();
foreach ($votes as $vote) {
echo "data: " . json_encode([
'type' => 'vote_update',
'track_id' => $vote['track_id'],
'vote_count' => $vote['vote_count']
]) . "\n\n";
}
flush();
$last_check = time();
}
// Send heartbeat every 30 seconds
if (time() % 30 == 0) {
echo ": heartbeat\n\n";
flush();
}
// Sleep for 1 second before next check
sleep(1);
}