{"id":30237,"date":"2025-12-30T16:57:39","date_gmt":"2025-12-30T11:27:39","guid":{"rendered":"https:\/\/opstree.com\/blog\/?p=30237"},"modified":"2025-12-30T16:57:39","modified_gmt":"2025-12-30T11:27:39","slug":"kafka-flask-real-time-streaming","status":"publish","type":"post","link":"https:\/\/opstree.com\/blog\/2025\/12\/30\/kafka-flask-real-time-streaming\/","title":{"rendered":"How to Stream Real-Time Playback Events to the Browser with Kafka and Flask"},"content":{"rendered":"<div style=\"background: #f8fafc; padding: 18px; border: 1px solid #e2e8f0; border-radius: 6px; font-family: Inter, Arial, sans-serif; margin: 20px 0;\">\n<h3 style=\"margin-top: 0; font-size: 18px;\">Table of Contents<\/h3>\n<ol style=\"margin: 0; padding-left: 18px; line-height: 1.6;\">\n<li><a style=\"text-decoration: none; color: #2563eb;\" href=\"#what-is-kafka\">What Is Kafka and Why Use It?<br \/>\n<\/a><\/li>\n<li><a style=\"text-decoration: none; color: #2563eb;\" href=\"#kafka-key-concepts\">Key Concepts of Kafka<br \/>\n<\/a><\/li>\n<li><a style=\"text-decoration: none; color: #2563eb;\" href=\"#why-kafka-streaming\">Why Kafka for Real-Time Streaming?<br \/>\n<\/a><\/li>\n<li><a style=\"text-decoration: none; color: #2563eb;\" href=\"#before-we-begin\">A Brief Thought Before We Begin<br \/>\n<\/a><\/li>\n<li><a style=\"text-decoration: none; color: #2563eb;\" href=\"#realtime-playback-events\">Real-Time Playback Events<br \/>\n<\/a><\/li>\n<li><a style=\"text-decoration: none; color: #2563eb;\" href=\"#tech-stack-overview\">Tech Stack Overview<br \/>\n<\/a><\/li>\n<li><a style=\"text-decoration: none; color: #2563eb;\" href=\"#event-dashboard\">Real-Time Playback Event Dashboard<\/a><\/li>\n<li><a style=\"text-decoration: none; color: #2563eb;\" href=\"#real-world-use-case\">Real-World Use Case<br \/>\n<\/a><\/li>\n<li><a style=\"text-decoration: none; color: #2563eb;\" href=\"#conclusion\">Conclusion<br \/>\n<\/a><\/li>\n<li><a style=\"text-decoration: none; color: #2563eb;\" href=\"#faqs\">Frequently Asked Questions<br \/>\n<\/a><\/li>\n<\/ol>\n<\/div>\n<p><!--more--><\/p>\n<p><!-- ===== TABLE OF CONTENTS END ===== --><\/p>\n<h2 id=\"what-is-kafka\" data-start=\"222\" data-end=\"258\"><strong data-start=\"225\" data-end=\"258\">What Is Kafka and Why Use It?<\/strong><\/h2>\n<p data-start=\"260\" data-end=\"710\">Apache Kafka is a powerful distributed event streaming platform that has become a cornerstone in modern data architectures. Designed for high-throughput and low-latency data pipelines, Kafka enables organizations to handle real-time data feeds efficiently. Its versatility makes it applicable across diverse industries, including finance, eCommerce, IoT, and system monitoring, where the ability to process and react to data in real-time is critical.<\/p>\n<p data-start=\"712\" data-end=\"868\">This document will explore the fundamental concepts of Kafka and delve into the reasons why it is the preferred choice for real-time streaming applications.<\/p>\n<h2 id=\"kafka-key-concepts\" data-start=\"1612\" data-end=\"1636\">Key Concepts of Kafka<\/h2>\n<p data-start=\"102\" data-end=\"321\">To understand the power and utility of Kafka, it\u2019s essential to grasp its core components and how they interact. These components work together to create a robust and scalable system for handling <a href=\"https:\/\/opstree.com\/blog\/2025\/11\/15\/real-time-data-processing\/\" target=\"_blank\" rel=\"noopener\">real-time data streams<\/a>.<\/p>\n<p data-start=\"102\" data-end=\"321\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-30246 size-full\" src=\"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/image-33.png\" alt=\"\" width=\"556\" height=\"457\" srcset=\"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/image-33.png 556w, https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/image-33-300x247.png 300w\" sizes=\"(max-width: 556px) 85vw, 556px\" \/><\/p>\n<h2 data-start=\"328\" data-end=\"346\"><strong data-start=\"331\" data-end=\"346\">Producer\u00a0<\/strong><\/h2>\n<p data-start=\"348\" data-end=\"840\">A producer is an application or system that sends data, often referred to as events or messages, to Kafka topics. Producers are responsible for serializing data and sending it to the appropriate topic. They don\u2019t need to know anything about the consumers or how the data will be used, which promotes decoupling and flexibility in the system. Producers can be configured to send data synchronously or asynchronously, depending on the application\u2019s requirements for reliability and performance.<\/p>\n<h2 data-start=\"847\" data-end=\"865\"><strong data-start=\"850\" data-end=\"865\">Consumer\u00a0<\/strong><\/h2>\n<p data-start=\"867\" data-end=\"1401\">A consumer is an application or system that reads data from Kafka topics. Consumers subscribe to one or more topics and receive messages as they are published. Like producers, consumers are decoupled from the rest of the system and can process data independently. Consumers can be grouped together to form consumer groups, which allow for parallel processing of data within a topic. Each consumer within a group is assigned a subset of the topic\u2019s partitions, ensuring that each message is processed by only one consumer in the group.<\/p>\n<h2 data-start=\"1408\" data-end=\"1423\"><strong data-start=\"1411\" data-end=\"1423\">Topic\u00a0<\/strong><\/h2>\n<p data-start=\"1425\" data-end=\"1837\">A topic is a named stream where messages are published and categorized. Think of it as a folder in a file system, but instead of files, it contains messages. Topics are the fundamental unit of organization in Kafka, allowing producers to send data to specific streams and consumers to subscribe to the streams they are interested in. Topics can have multiple partitions, which enable parallelism and scalability.<\/p>\n<h2 data-start=\"1844\" data-end=\"1861\"><strong data-start=\"1847\" data-end=\"1861\">Broker\u00a0<\/strong><\/h2>\n<p data-start=\"1863\" data-end=\"2349\">A broker is a <a href=\"https:\/\/opstree.com\/blog\/2021\/02\/02\/introduction-to-kafka-otkafkadiaries\/\" target=\"_blank\" rel=\"noopener\">Kafka server<\/a> that stores and serves messages. Kafka clusters consist of one or more brokers, which work together to manage the data and handle requests from producers and consumers. Brokers are responsible for storing messages, replicating data across the cluster for fault tolerance, and handling requests from producers and consumers. Each broker in the cluster stores a portion of the data for each topic, and the cluster as a whole provides a unified view of the data.<\/p>\n<h2 data-start=\"2356\" data-end=\"2375\"><strong data-start=\"2359\" data-end=\"2375\">Partition\u00a0<\/strong><\/h2>\n<p data-start=\"2377\" data-end=\"3018\">Partitions are the key to Kafka\u2019s scalability and parallelism. A topic is divided into one or more partitions, each of which is an ordered, immutable sequence of messages. Each partition is stored on one or more brokers in the Kafka cluster. Producers send messages to specific partitions within a topic, and consumers read messages from specific partitions. By dividing a topic into multiple partitions, Kafka can distribute the load across multiple brokers, allowing for higher throughput and lower latency. Partitions also enable parallel processing of data by allowing multiple consumers to read from different partitions simultaneously.<\/p>\n<div style=\"border: 1px solid #d1d5db; padding: 16px; margin: 20px 0; background-color: #f0f4f8;\">\n<p style=\"margin: 0; font-weight: 600; font-size: 16px;\">Also Read &#8211; <a href=\"https:\/\/opstree.com\/blog\/2025\/11\/11\/complete-guide-to-data-pipelines\/\" target=\"_blank\" rel=\"noopener\">The Complete Guide To Data Pipelines With Architecture, Types and Use Cases <\/a><\/p>\n<\/div>\n<h2 id=\"why-kafka-streaming\" data-start=\"91\" data-end=\"132\"><strong data-start=\"94\" data-end=\"132\">Why Kafka for Real-Time Streaming?<\/strong><\/h2>\n<p data-start=\"134\" data-end=\"398\">Kafka has emerged as the go-to solution for <a href=\"https:\/\/opstree.com\/blog\/2025\/12\/09\/azure-event-hubs-real-time-streaming\/\" target=\"_blank\" rel=\"noopener\">real-time streaming<\/a> applications due to its unique combination of features and capabilities. It addresses the challenges of handling high-volume, high-velocity data streams with reliability, scalability, and flexibility.<\/p>\n<p data-start=\"134\" data-end=\"398\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-30248 aligncenter\" src=\"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/image-35.png\" alt=\"\" width=\"513\" height=\"414\" srcset=\"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/image-35.png 513w, https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/image-35-300x242.png 300w\" sizes=\"(max-width: 513px) 85vw, 513px\" \/><\/p>\n<h3 data-start=\"405\" data-end=\"427\"><strong data-start=\"409\" data-end=\"427\">Scalability\u00a0<\/strong><\/h3>\n<p data-start=\"429\" data-end=\"990\">Kafka is designed to handle massive amounts of data with ease. Its distributed architecture allows it to scale horizontally by adding more brokers to the cluster. Each broker can handle a portion of the overall load, allowing Kafka to handle millions of messages per second. The partitioning of topics further enhances scalability by allowing multiple consumers to process data in parallel. This scalability makes Kafka suitable for applications that need to handle large volumes of data, such as social media feeds, IoT sensor data, and financial transactions.<\/p>\n<h3 data-start=\"997\" data-end=\"1018\"><strong data-start=\"1001\" data-end=\"1018\">Durability\u00a0<\/strong><\/h3>\n<p data-start=\"1020\" data-end=\"1669\">Data durability is a critical requirement for many real-time streaming applications. Kafka ensures durability by persisting messages to disk and replicating them across multiple brokers. This means that even if one or more brokers fail, the data is still available and can be recovered. The replication factor determines the number of copies of each message that are stored in the cluster. A higher replication factor provides greater durability but also requires more storage space. Kafka\u2019s durability features make it suitable for applications that cannot afford to lose data, such as financial transactions and critical infrastructure monitoring.<\/p>\n<h3 data-start=\"1676\" data-end=\"1697\"><strong data-start=\"1680\" data-end=\"1697\">Decoupling\u00a0<\/strong><\/h3>\n<p data-start=\"1699\" data-end=\"2235\">Kafka promotes decoupling between producers and consumers, which makes the system more flexible and resilient. Producers and consumers operate independently and do not need to know anything about each other. Producers simply send messages to Kafka topics, and consumers subscribe to the topics they are interested in. This decoupling allows producers and consumers to evolve independently without affecting each other. It also makes it easier to add or remove producers and consumers from the system without disrupting the flow of data.<\/p>\n<h3 data-start=\"2242\" data-end=\"2263\"><strong data-start=\"2246\" data-end=\"2263\">Low Latency\u00a0<\/strong><\/h3>\n<p data-start=\"2265\" data-end=\"2771\">Low latency is essential for real-time applications that need to react to events quickly. Kafka is designed for low-latency data delivery, with typical end-to-end latencies of just a few milliseconds. This low latency is achieved through a combination of factors, including its efficient storage format, its use of zero-copy data transfer, and its ability to process data in parallel. Kafka\u2019s low latency makes it suitable for applications such as fraud detection, <a href=\"https:\/\/opstree.com\/blog\/2025\/11\/15\/real-time-data-processing\/\" target=\"_blank\" rel=\"noopener\">real-time analytics<\/a>, and live dashboards.<\/p>\n<h2 id=\"before-we-begin\" data-start=\"2778\" data-end=\"2819\"><strong data-start=\"2781\" data-end=\"2819\">A Brief Thought Before We Begin\u00a0<\/strong><\/h2>\n<p data-start=\"2821\" data-end=\"3034\">In today\u2019s digital world, users expect instant feedback, whether they\u2019re watching a show, tracking a delivery, or monitoring a system. Real-time updates aren\u2019t just a luxury anymore; they\u2019re a baseline expectation.<\/p>\n<p data-start=\"3036\" data-end=\"3264\">Before we explore how Netflix handles playback events, I want to walk you through a simple yet powerful setup I built using Flask, Kafka, and Socket.IO, a lightweight stack that delivers real-time updates straight to the browser.<\/p>\n<div style=\"border: 1px solid #d1d5db; padding: 16px; margin: 20px 0; background-color: #f0f4f8;\">\n<p style=\"margin: 0; font-weight: 600; font-size: 16px;\">Case Study : <a href=\"https:\/\/opstree.com\/case-study\/accelerating-a-global-tech-leaders-ads-platform-with-strategic-devops-platform-and-data-engineering\/\" target=\"_blank\" rel=\"noopener\">Accelerating a Global Tech Leader\u2019s Ads Platform with Strategic DevOps, Platform, and Data Engineering<\/a><\/p>\n<\/div>\n<h2 id=\"realtime-playback-events\" data-start=\"3271\" data-end=\"3306\"><strong data-start=\"3274\" data-end=\"3306\">Real-Time Playback Events\u00a0<\/strong><\/h2>\n<h3 data-start=\"3308\" data-end=\"3339\"><strong data-start=\"3312\" data-end=\"3339\">The Netflix Inspiration<\/strong><\/h3>\n<p data-start=\"3341\" data-end=\"3666\">Netflix tracks every playback event pause, play, seek, stop to improve user experience, monitor performance, and personalize recommendations. These events are streamed in real time using a robust data pipeline powered by Kafka. Inspired by this, I built a minimal version of this system to understand the mechanics behind it.<\/p>\n<h2 id=\"tech-stack-overview\" data-start=\"3673\" data-end=\"3702\"><strong data-start=\"3676\" data-end=\"3702\">Tech Stack Overview\u00a0<\/strong><\/h2>\n<p data-start=\"3704\" data-end=\"3952\"><strong data-start=\"3704\" data-end=\"3716\">Flask <\/strong>: Lightweight Python web framework for serving the backend<br data-start=\"3774\" data-end=\"3777\" \/><strong data-start=\"3777\" data-end=\"3789\">Kafka <\/strong>: Distributed event streaming platform for handling playback events<br data-start=\"3856\" data-end=\"3859\" \/><strong data-start=\"3859\" data-end=\"3875\">Socket.IO <\/strong>: Enables real-time, bidirectional communication between server and browser<\/p>\n<h3 data-start=\"3959\" data-end=\"3981\"><strong data-start=\"3963\" data-end=\"3981\">What Is Flask?<\/strong><\/h3>\n<p data-start=\"3983\" data-end=\"4234\">Flask is a micro web framework written in Python. It\u2019s designed to be simple and flexible, making it ideal for small projects and quick prototypes. In this setup, Flask serves the HTML page and integrates with Socket.IO to push updates to the browser.<\/p>\n<ul>\n<li data-start=\"4238\" data-end=\"4268\">Minimal setup, easy to learn<\/li>\n<li data-start=\"4271\" data-end=\"4311\">Great for REST APIs and real-time apps<\/li>\n<li data-start=\"4314\" data-end=\"4359\">Extensible with plugins like Flask-SocketIO<\/li>\n<\/ul>\n<h3 data-start=\"4366\" data-end=\"4392\"><strong data-start=\"4370\" data-end=\"4392\">What Is Socket.IO?<\/strong><\/h3>\n<p data-start=\"4394\" data-end=\"4558\">Socket.IO is a library that enables real-time, bidirectional communication between the server and browser. It uses WebSockets under the hood but adds features like:<\/p>\n<ul>\n<li data-start=\"4562\" data-end=\"4586\">Automatic reconnection<\/li>\n<li data-start=\"4589\" data-end=\"4612\">Event-based messaging<\/li>\n<li data-start=\"4615\" data-end=\"4645\">Fallbacks for older browsers<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-30249 aligncenter\" src=\"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/image-36.png\" alt=\"\" width=\"612\" height=\"459\" srcset=\"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/image-36.png 612w, https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/image-36-300x225.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<h2 id=\"event-dashboard\" data-start=\"70\" data-end=\"112\"><strong data-start=\"74\" data-end=\"112\">Real-Time Playback Event Dashboard<\/strong><\/h2>\n<p data-start=\"114\" data-end=\"313\" data-is-last-node=\"\" data-is-only-node=\"\">The UI displays live playback events streamed from Kafka to the browser using Flask and Socket.IO. Each event, whether it\u2019s a pause, play, buffer_start, or buffer_end\u2014is instantly reflected in the UI.<\/p>\n<p data-start=\"114\" data-end=\"313\" data-is-last-node=\"\" data-is-only-node=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-30251 size-full aligncenter\" src=\"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-11-19-131955.png\" alt=\"\" width=\"729\" height=\"612\" srcset=\"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-11-19-131955.png 729w, https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-11-19-131955-300x252.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p data-start=\"114\" data-end=\"313\" data-is-last-node=\"\" data-is-only-node=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-30250 size-large\" src=\"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-11-19-132024-1024x511.png\" alt=\"\" width=\"840\" height=\"419\" srcset=\"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-11-19-132024-1024x511.png 1024w, https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-11-19-132024-300x150.png 300w, https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-11-19-132024-768x383.png 768w, https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-11-19-132024-1200x599.png 1200w, https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-11-19-132024.png 1232w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p data-start=\"70\" data-end=\"280\">This dashboard displays live playback events streamed from Kafka to the browser using Flask and Socket.IO. Each row represents a unique event\u2014such as <strong data-start=\"220\" data-end=\"228\">play<\/strong>, <strong data-start=\"230\" data-end=\"239\">pause<\/strong>, or <strong data-start=\"244\" data-end=\"252\">seek <\/strong>and includes metadata like:<\/p>\n<ul>\n<li data-start=\"284\" data-end=\"308\"><strong data-start=\"287\" data-end=\"299\">Movie ID<\/strong><\/li>\n<li data-start=\"311\" data-end=\"334\"><strong data-start=\"314\" data-end=\"325\">User ID<\/strong> (<\/li>\n<li data-start=\"337\" data-end=\"372\"><strong data-start=\"340\" data-end=\"361\">Playback Position<\/strong> in seconds<\/li>\n<\/ul>\n<p><strong>producer.py<\/strong><\/p>\n<pre style=\"background: #0f172a; color: #e5e7eb; padding: 16px; border-radius: 8px; font-size: 14px; line-height: 1.6; overflow-x: auto; max-width: 100%;\">from kafka import KafkaProducer\r\nimport json\r\nimport random\r\nimport time\r\nfrom datetime import datetime\r\n\r\n# Create Kafka producer\r\nproducer = KafkaProducer(\r\n    bootstrap_servers=['localhost:9092'],\r\n    value_serializer=lambda v: json.dumps(v).encode('utf-8')\r\n)\r\n\r\n# Sample data\r\nmovies = [f\"MOV{i:03d}\" for i in range(1, 21)]\r\nevents = [\"play\", \"pause\", \"stop\", \"seek\", \"buffer_start\", \"buffer_end\"]\r\ndevices = [\"web\", \"tv\", \"mobile\"]\r\nbitrates = [600, 1200, 2400, 4800]\r\n\r\ndef random_event():\r\n    return {\r\n        \"event_id\": f\"evt-{int(time.time() * 1000)}\",\r\n        \"user_id\": f\"user_{random.randint(1, 50)}\",\r\n        \"movie_id\": random.choice(movies),\r\n        \"event_type\": random.choice(events),\r\n        \"position_seconds\": random.randint(0, 7200),\r\n        \"bitrate_kbps\": random.choice(bitrates),\r\n        \"device\": random.choice(devices),\r\n        \"ts\": datetime.utcnow().isoformat() + \"Z\"\r\n    }\r\n\r\n# Continuously send events\r\nwhile True:\r\n    evt = random_event()\r\n    producer.send(\"playback-events\", evt)\r\n    print(\"Sent:\", evt[\"event_type\"], evt[\"movie_id\"])\r\n    time.sleep(random.choice([0.2, 0.5, 1]))\r\n<\/pre>\n<p><strong>consumer_server.py<\/strong><\/p>\n<pre style=\"background: #0f172a; color: #e5e7eb; padding: 16px; border-radius: 8px; font-size: 14px; line-height: 1.6; overflow-x: auto; max-width: 100%;\">import json\r\nfrom threading import Thread\r\nfrom kafka import KafkaConsumer\r\nfrom flask import Flask, render_template\r\nfrom flask_socketio import SocketIO\r\n\r\napp = Flask(__name__, template_folder='templates')\r\nsocketio = SocketIO(app, cors_allowed_origins=\"*\")\r\n\r\nKAFKA_TOPIC = \"playback-events\"\r\nKAFKA_BOOTSTRAP = \"localhost:9092\"\r\n\r\ndef kafka_listener():\r\n    consumer = KafkaConsumer(\r\n        KAFKA_TOPIC,\r\n        bootstrap_servers=[KAFKA_BOOTSTRAP],\r\n        value_deserializer=lambda m: json.loads(m.decode('utf-8')),\r\n        auto_offset_reset='latest',\r\n        enable_auto_commit=True,\r\n        group_id=\"realtime-demo\"\r\n    )\r\n\r\n    print(\"Kafka consumer started listening on\", KAFKA_TOPIC)\r\n\r\n    for msg in consumer:\r\n        event = msg.value\r\n        print(\"Received:\", event)\r\n        socketio.emit('new_event', event)\r\n\r\n@app.route('\/')\r\ndef index():\r\n    return render_template('index.html')\r\n\r\nif __name__ == \"__main__\":\r\n    Thread(target=kafka_listener, daemon=True).start()\r\n    socketio.run(app, host='0.0.0.0', port=5050)\r\n<\/pre>\n<p><strong>templates\/index.html<\/strong><\/p>\n<pre style=\"background: #0f172a; color: #e5e7eb; padding: 16px; border-radius: 8px; font-size: 14px; line-height: 1.6; overflow-x: auto; max-width: 100%;\">&lt;!doctype html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=\"utf-8\"&gt;\r\n&lt;title&gt;Real-Time Playback Events&lt;\/title&gt;\r\n\r\n&lt;style&gt;\r\nbody {\r\n  font-family: sans-serif;\r\n  background: #f4f4f4;\r\n  padding: 20px;\r\n}\r\n\r\n#events {\r\n  display: flex;\r\n  flex-direction: column;\r\n}\r\n\r\n.evt {\r\n  background: #fff;\r\n  padding: 10px;\r\n  margin-bottom: 8px;\r\n  border-left: 6px solid #333;\r\n}\r\n\r\n.play { border-color: #22c55e; }\r\n.pause { border-color: #facc15; }\r\n.stop { border-color: #ef4444; }\r\n.seek { border-color: #3b82f6; }\r\n.buffer_start { border-color: #a855f7; }\r\n.buffer_end { border-color: #6366f1; }\r\n\r\n.meta {\r\n  font-size: 12px;\r\n  color: #666;\r\n}\r\n&lt;\/style&gt;\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n&lt;h2&gt;Real-Time Playback Events&lt;\/h2&gt;\r\n&lt;div id=\"status\"&gt;Connecting...&lt;\/div&gt;\r\n&lt;div id=\"events\"&gt;&lt;\/div&gt;\r\n\r\n&lt;script src=\"https:\/\/cdn.socket.io\/4.6.1\/socket.io.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\nconst socket = io();\r\nconst status = document.getElementById('status');\r\nconst eventsDiv = document.getElementById('events');\r\n\r\nsocket.on('connect', () =&gt; {\r\n  status.textContent = 'Connected to server';\r\n  status.style.color = 'green';\r\n});\r\n\r\nsocket.on('disconnect', () =&gt; {\r\n  status.textContent = 'Disconnected';\r\n  status.style.color = 'red';\r\n});\r\n\r\nsocket.on('new_event', (evt) =&gt; {\r\n  const el = document.createElement('div');\r\n  el.className = 'evt ' + evt.event_type;\r\n\r\n  el.innerHTML = `\r\n    &lt;div&gt;&lt;strong&gt;${evt.event_type.toUpperCase()}&lt;\/strong&gt; \u2013 ${evt.movie_id}&lt;\/div&gt;\r\n    &lt;div class=\"meta\"&gt;\r\n      User: ${evt.user_id} |\r\n      Position: ${evt.position_seconds}s |\r\n      Device: ${evt.device}\r\n    &lt;\/div&gt;\r\n  `;\r\n\r\n  eventsDiv.prepend(el);\r\n\r\n  if (eventsDiv.children.length &gt; 100) {\r\n    eventsDiv.removeChild(eventsDiv.lastChild);\r\n  }\r\n});\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>1. Start Kafka locally<\/strong><\/p>\n<pre style=\"background: #0f172a; color: #e5e7eb; padding: 16px; border-radius: 8px; font-size: 14px; line-height: 1.6; overflow-x: auto; max-width: 100%;\"># Start Zookeeper\r\nbin\/zookeeper-server-start.sh config\/zookeeper.properties\r\n\r\n# Start Kafka broker\r\nbin\/kafka-server-start.sh config\/server.properties\r\n\r\n# Create topic\r\nbin\/kafka-topics.sh --create \\\r\n  --topic playback-events \\\r\n  --bootstrap-server localhost:9092 \\\r\n  --partitions 3 \\\r\n  --replication-factor 1\r\n<\/pre>\n<p data-start=\"479\" data-end=\"511\"><strong data-start=\"482\" data-end=\"511\">2. Run the Kafka Producer<\/strong><\/p>\n<pre style=\"background: #0f172a; color: #e5e7eb; padding: 16px; border-radius: 8px; font-size: 14px; line-height: 1.6; overflow-x: auto; max-width: 100%;\">python3 producer.py\r\n<\/pre>\n<p data-start=\"479\" data-end=\"511\"><strong data-start=\"554\" data-end=\"583\">3. Start the Flask Server<\/strong><\/p>\n<div class=\"contain-inline-size rounded-2xl corner-superellipse\/1.1 relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\">\n<pre style=\"background: #0f172a; color: #e5e7eb; padding: 16px; border-radius: 8px; font-size: 14px; line-height: 1.6; overflow-x: auto; max-width: 100%;\">python consumer_server.py<\/pre>\n<\/div>\n<\/div>\n<div dir=\"ltr\">\n<p data-start=\"629\" data-end=\"650\"><strong data-start=\"632\" data-end=\"650\">4. Open the UI<\/strong><\/p>\n<p data-start=\"652\" data-end=\"701\">Visit <strong data-start=\"658\" data-end=\"684\"><a class=\"decorated-link cursor-pointer\" target=\"_new\" rel=\"noopener\" data-start=\"660\" data-end=\"682\">https:\/\/localhost:5050<\/a><\/strong> in your browser.<\/p>\n<h2 data-start=\"1787\" data-end=\"1815\"><strong id=\"real-world-use-case\" data-start=\"1792\" data-end=\"1815\">Real-World Use Case<\/strong><\/h2>\n<p data-start=\"1817\" data-end=\"2088\">In one of my internal monitoring dashboards, I used this setup to stream real-time logs from a Kafka topic to a browser-based UI. This allowed my team to visualize playback events and system behavior without refreshing the page, a huge win for <a href=\"https:\/\/opstree.com\/services\/observability-sre-production-engineering\/\" target=\"_blank\" rel=\"noopener\">observability<\/a> and debugging.<\/p>\n<h2 data-start=\"708\" data-end=\"728\"><strong id=\"conclusion\" data-start=\"714\" data-end=\"728\">Conclusion<\/strong><\/h2>\n<p data-start=\"730\" data-end=\"1147\">Building a real-time playback monitoring system doesn\u2019t require massive infrastructure or complex tooling. With just <strong data-start=\"847\" data-end=\"878\">Kafka, Flask, and Socket.IO<\/strong>, you can simulate the same core mechanics used by large-scale platforms like <a href=\"https:\/\/www.netflix.com\/in\/\" target=\"_blank\" rel=\"noopener\">Netflix<\/a>. Kafka handles the event streaming, Flask acts as the lightweight backend, and Socket.IO ensures instant browser updates, together creating a seamless, reactive, and scalable pipeline.<\/p>\n<p data-start=\"1149\" data-end=\"1530\">This architecture not only helps you understand how modern streaming giants process billions of events but also gives you a practical foundation for building real-time dashboards, monitoring tools, or live analytics applications. As user experiences become increasingly interactive and time-sensitive, mastering event-driven, real-time systems is no longer optional, it\u2019s essential.<\/p>\n<p data-start=\"1532\" data-end=\"1780\">By experimenting with this mini playback pipeline, you\u2019ve taken a meaningful step into the world of <a href=\"https:\/\/opstree.com\/services\/database-and-data-engineering\/\" target=\"_blank\" rel=\"noopener\"><strong data-start=\"1632\" data-end=\"1662\">real-time data engineering<\/strong><\/a>. Keep exploring, keep building, and keep pushing the boundaries of what can be done with streaming technologies.<\/p>\n<\/div>\n<h2 data-start=\"2095\" data-end=\"2137\"><strong id=\"faqs\" data-start=\"2100\" data-end=\"2137\">Frequently Asked Questions\u00a0<\/strong><\/h2>\n<h3 data-start=\"2139\" data-end=\"2187\"><strong data-start=\"2143\" data-end=\"2187\">1. What is Kafka used for in this setup?<\/strong><\/h3>\n<p data-start=\"2188\" data-end=\"2289\">Kafka acts as a message broker that streams playback events from producers to consumers in real time.<\/p>\n<h3 data-start=\"2296\" data-end=\"2352\"><strong data-start=\"2300\" data-end=\"2352\">2. Why use Flask instead of a heavier framework?<\/strong><\/h3>\n<p data-start=\"2353\" data-end=\"2469\">Flask is lightweight and easy to integrate with Socket.IO, making it ideal for quick prototyping and real-time apps.<\/p>\n<h3 data-start=\"2476\" data-end=\"2530\"><strong data-start=\"2480\" data-end=\"2530\">3. Can this architecture scale for production?<\/strong><\/h3>\n<p data-start=\"2531\" data-end=\"2655\">Yes, with proper load balancing, Kafka partitioning, and a production-grade WSGI server like Gunicorn, this setup can scale.<\/p>\n<h3 data-start=\"2662\" data-end=\"2715\"><strong data-start=\"2666\" data-end=\"2715\">4. How does Socket.IO differ from WebSockets?<\/strong><\/h3>\n<p data-start=\"2716\" data-end=\"2842\">Socket.IO builds on WebSockets but adds features like automatic reconnection, fallback options, and event-based communication.<\/p>\n<h3 data-start=\"2849\" data-end=\"2912\"><strong data-start=\"2853\" data-end=\"2912\">5. What are some real-world use cases for this pattern?<\/strong><\/h3>\n<p data-start=\"2913\" data-end=\"3039\">Live dashboards, chat applications, collaborative tools, and real-time analytics platforms all benefit from this architecture.<\/p>\n<p data-start=\"2913\" data-end=\"3039\">Related Searches &#8211; <a href=\"https:\/\/opstree.com\/blog\/2023\/06\/20\/database-migration-service-in-aws\/\" target=\"_blank\" rel=\"noopener\">AWS\u00a0Database Migration Service<\/a> | <a href=\"https:\/\/opstree.com\/blog\/2025\/10\/27\/data-engineering-companies\/\" target=\"_blank\" rel=\"noopener\">Best data engineering company in India<\/a> | <a href=\"https:\/\/opstree.com\/services\/middleware-database-and-data-engineering\/\" target=\"_blank\" rel=\"noopener\">Data pipeline development services<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of Contents What Is Kafka and Why Use It? Key Concepts of Kafka Why Kafka for Real-Time Streaming? A Brief Thought Before We Begin Real-Time Playback Events Tech Stack Overview Real-Time Playback Event Dashboard Real-World Use Case Conclusion Frequently Asked Questions<\/p>\n","protected":false},"author":244582717,"featured_media":30261,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[768739361],"tags":[3768,768739405,768739342,207392,343865],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/12\/Blog-Image-Template-11.jpg","jetpack_likes_enabled":false,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfDBOm-7RH","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/30237"}],"collection":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/users\/244582717"}],"replies":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/comments?post=30237"}],"version-history":[{"count":14,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/30237\/revisions"}],"predecessor-version":[{"id":30263,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/30237\/revisions\/30263"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media\/30261"}],"wp:attachment":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media?parent=30237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/categories?post=30237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/tags?post=30237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}