Files
crm.clientright.ru/crm_extensions/file_storage/api/redis_sse_predis.php
Fedor 9245768987 🚀 CRM Files Migration & Real-time Features
 Features:
- Migrated ALL files to new S3 structure (Projects, Contacts, Accounts, HelpDesk, Invoice, etc.)
- Added Nextcloud folder buttons to ALL modules
- Fixed Nextcloud editor integration
- WebSocket server for real-time updates
- Redis Pub/Sub integration
- File path manager for organized storage
- Redis caching for performance (Functions.php)

📁 New Structure:
Documents/Project/ProjectName_ID/file_docID.ext
Documents/Contacts/FirstName_LastName_ID/file_docID.ext
Documents/Accounts/AccountName_ID/file_docID.ext

🔧 Technical:
- FilePathManager for standardized paths
- S3StorageService integration
- WebSocket server (Node.js + Docker)
- Redis cache for getBasicModuleInfo()
- Predis library for Redis connectivity

📝 Scripts:
- Migration scripts for all modules
- Test pages for WebSocket/SSE/Polling
- Documentation (MIGRATION_*.md, REDIS_*.md)

🎯 Result: 15,000+ files migrated successfully!
2025-10-24 19:59:28 +03:00

99 lines
3.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* SSE Subscriber: Redis → Browser (через Predis)
*
* Использует Predis вместо расширения Redis для совместимости
*/
// Отключаем буферизацию
while (@ob_end_flush());
// Настройки SSE
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('Access-Control-Allow-Origin: *');
header('X-Accel-Buffering: no');
// Отключаем лимит времени
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
set_time_limit(0);
ignore_user_abort(false);
// Функция для отправки события
function send($type, $data) {
echo "data: " . json_encode([
'type' => $type,
'data' => $data,
'time' => date('H:i:s')
]) . "\n\n";
flush();
}
try {
// Логируем начало
error_log("[SSE] Starting SSE connection at " . date('Y-m-d H:i:s'));
// Подключаем Predis через Composer
require_once '/var/www/fastuser/data/www/crm.clientright.ru/vendor/autoload.php';
error_log("[SSE] Autoloader loaded");
// Создаем клиент Predis
$redis = new Predis\Client([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'password' => 'CRM_Redis_Pass_2025_Secure!',
'database' => 0,
]);
error_log("[SSE] Predis client created");
// Пробуем ping
$pong = $redis->ping();
error_log("[SSE] Redis PING: " . ($pong ? 'PONG' : 'FAILED'));
// СРАЗУ отправляем начальное событие
send('connected', ['message' => 'Подключено к Redis через Predis', 'timestamp' => time()]);
error_log("[SSE] Connected event sent");
// Отправляем heartbeat каждые 15 секунд
$lastHeartbeat = time();
// Подписываемся на канал
$channel = 'crm:file:events';
$pubsub = $redis->pubSubLoop();
$pubsub->subscribe($channel);
foreach ($pubsub as $message) {
// Heartbeat для поддержания соединения
if (time() - $lastHeartbeat > 15) {
send('heartbeat', ['timestamp' => time()]);
$lastHeartbeat = time();
}
// Обрабатываем только сообщения (не subscribe/unsubscribe)
if ($message->kind === 'message') {
// Декодируем событие
$event = json_decode($message->payload, true);
if ($event && isset($event['type']) && isset($event['data'])) {
// Отправляем событие клиенту
send($event['type'], $event['data']);
}
}
// Проверяем не отключился ли клиент
if (connection_aborted()) {
break;
}
}
} catch (Exception $e) {
send('error', ['message' => 'Redis error: ' . $e->getMessage()]);
}
?>