PHP पर WebSocket: उन्नत स्तर के real-time अनुप्रयोग बनाना

शुरुआती लोगों के लिए ऑनलाइन Python प्रशिक्षक

Python आसानी से सीखें बिना सिद्धांत के बोझ के। स्वचालित जांच के साथ व्यावहारिक कार्य हल करें और ब्राउज़र में सीधे कोड लिखें।

पाठ्यक्रम शुरू करें

PHP पर WebSocket: सिद्धांत से औद्योगिक real-time अनुप्रयोग तक



WebSocket प्रोटोकॉल ने वेब डेवलपमेंट में क्रांति ला दी है, जिससे क्लाइंट और सर्वर के बीच पूर्ण द्विदिश संचार चैनल बनाना संभव हो गया है। क्लासिक AJAX (long polling, Server-Sent Events) के विपरीत, WebSocket न्यूनतम ओवरहेड के साथ एक स्थायी कनेक्शन प्रदान करता है। PHP डेवलपर के लिए, बुनियादी "इको-सर्वर" उदाहरणों से औद्योगिक real-time अनुप्रयोग में संक्रमण एक गंभीर चुनौती है, जो स्थिति प्रबंधन, स्केलिंग और एसिंक्रोनस प्रोसेसिंग से संबंधित है।



इस लेख में, हम PHP पर WebSocket के साथ काम करने की उन्नत तकनीकों पर विचार करेंगे: मल्टी-यूज़र चैट आर्किटेक्चर, क्षैतिज स्केलिंग के लिए Redis के साथ एकीकरण, बैकग्राउंड कार्यों की प्रोसेसिंग और प्रदर्शन अनुकूलन।



1. Ratchet पर उन्नत WebSocket सर्वर की आर्किटेक्चर



लाइब्रेरी Ratchet (ReactPHP पर आधारित) PHP पर WebSocket के लिए स्वर्ण मानक बनी हुई है। हालांकि, production समाधानों के लिए सरल onMessage पर्याप्त नहीं है। आइए जिम्मेदारी के पृथक्करण के साथ घटक आर्किटेक्चर पर विचार करें:



<?phpuse Ratchet\\MessageComponentInterface;use Ratchet\\ConnectionInterface;use React\\EventLoop\\LoopInterface;

class RealTimeHub implements MessageComponentInterface { private $clients; private $loop; private $redis; private $userRegistry;

public function __construct(LoopInterface $loop, \\Redis $redis) { $this->clients = new \\SplObjectStorage; $this->loop = $loop; $this->redis = $redis; $this->userRegistry = []; }

public function onOpen(ConnectionInterface $conn) { $this->clients->attach($conn); echo "नया कनेक्शन: {$conn->resourceId}"; }

public function onMessage(ConnectionInterface $from, $msg) { $data = json_decode($msg, true);

switch ($data['type'] ?? '') { case 'auth': $this->handleAuth($from, $data); break; case 'message': $this->handleMessage($from, $data); break; case 'subscribe': $this->handleSubscribe($from, $data); break; default: $from->send(json_encode(['error' => 'अज्ञात कमांड'])); } }

public function onClose(ConnectionInterface $conn) { $this->clients->detach($conn); // उपयोगकर्ता पंजीकरण साफ़ करें if (isset($conn->userId)) { unset($this->userRegistry[$conn->userId]); } }

public function onError(ConnectionInterface $conn, \\Exception $e) { $conn->close(); }

private function handleAuth(ConnectionInterface $conn, array $data) { $token = $data['token'] ?? ''; // JWT-टोकन की जाँच $userId = $this->validateToken($token); if ($userId) { $conn->userId = $userId; $this->userRegistry[$userId] = $conn; $conn->send(json_encode(['type' => 'auth_ok'])); } else { $conn->send(json_encode(['type' => 'auth_fail'])); } }

private function handleMessage(ConnectionInterface $from, array $data) { $targetUserId = $data['to'] ?? null; if ($targetUserId && isset($this->userRegistry[$targetUserId])) { $this->userRegistry[$targetUserId]->send(json_encode([ 'type' => 'message', 'from' => $from->userId ?? 'अनाम', 'text' => $data['text'] ])); } }

private function validateToken(string $token): ?int { // JWT सत्यापन का अनुकरण $payload = json_decode(base64_decode(explode('.', $token)[1] ?? ''), true); return $payl

ब्लॉग

पुस्तक अनुशंसाएँ