feat: Implement zero-latency WebSocket live message broadcast with luxury audio chime and Apple-style toast notifications

This commit is contained in:
Hamza-Ayed
2026-08-27 15:22:24 +03:00
parent 7faf223583
commit 70176deb3b
3 changed files with 248 additions and 19 deletions
+42 -14
View File
@@ -312,26 +312,54 @@ class ChatController
[$uuid, $userId, $receiverId, $courseId, $lessonId, $messageText, $messageType, $mediaUrl]
);
$msgPayload = [
'id' => $messageId,
'uuid' => $uuid,
'sender_id' => $userId,
'receiver_id' => $receiverId,
'is_mine' => false,
'course_id' => $courseId,
'lesson_id' => $lessonId,
'message' => $messageText,
'message_type' => $messageType,
'media_url' => $mediaUrl,
'is_read' => false,
'created_at' => date('Y-m-d H:i:s'),
];
// Push directly to Workerman WebSocket server for instant 0ms live broadcast to receiver
self::pushToWorkerman('push_chat', $receiverId, $msgPayload);
$response->status(201)->json([
'status' => 'success',
'message' => 'تم إرسال الرسالة بنجاح',
'data' => [
'id' => $messageId,
'uuid' => $uuid,
'sender_id' => $userId,
'receiver_id' => $receiverId,
'is_mine' => true,
'course_id' => $courseId,
'lesson_id' => $lessonId,
'message' => $messageText,
'message_type' => $messageType,
'media_url' => $mediaUrl,
'is_read' => false,
'created_at' => date('Y-m-d H:i:s'),
]
'data' => array_merge($msgPayload, ['is_mine' => true])
]);
}
/**
* Push internal event to Workerman's internal HTTP server (port 4241)
*/
public static function pushToWorkerman(string $action, int $targetUserId, array $payload): bool
{
try {
$ch = curl_init('http://127.0.0.1:4241');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'action' => $action,
'target_user_id' => $targetUserId,
'payload' => json_encode($payload, JSON_UNESCAPED_UNICODE)
]));
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 400); // Fast non-blocking timeout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
return true;
} catch (\Throwable $e) {
return false;
}
}
/**
* Mark all messages in a conversation as read
* POST /api/chat/read