Menu

1 / 2

WebSocket Real-Time Notifications for Video Platforms with Python

DEV Community·ahmet gedik·6 months ago
#oTfKfCH5
#realtime#python#websockets#tutorial#region
Reading 0:00
15s threshold

Real-Time in a Multi-Region Video Platform

When a viral news clip appears in the UAE or a Danish documentary breaks into Nordic trending charts, TrendVidStream users should see it instantly. A Python WebSocket server bridges the gap between the PHP cron fetcher and the browser.

Architecture

PHP Cron Fetcher ──→ Redis Pub/Sub ──→ Python WS Server ──→ Browser clients (8 regions) (one channel per region) (filtered by locale)

Enter fullscreen mode

Exit fullscreen mode

Python WebSocket Server

pip install websockets redis asyncio

Enter fullscreen mode

Exit fullscreen mode

import asyncio import json import logging import urllib.parse import redis.asyncio as aioredis import websockets from websockets.server import WebSocketServerProtocol

logger = logging . getLogger ( __name__ ) logging . basicConfig ( level = logging . INFO )

region_clients : dict [ str , set [ WebSocketServerProtocol ]] = {}

async def broadcast_to_region ( region : str , message : str ) -> None : clients = region_clients .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.