<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hitesh Narwal on Medium]]></title>
        <description><![CDATA[Stories by Hitesh Narwal on Medium]]></description>
        <link>https://medium.com/@hitesh4296?source=rss-f9240e7b4b01------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-cxuWzhF7oeDEGfylOZDbg.png</url>
            <title>Stories by Hitesh Narwal on Medium</title>
            <link>https://medium.com/@hitesh4296?source=rss-f9240e7b4b01------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:11:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hitesh4296/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Server-Sent Events: Breaking Down How ChatGPT Streams Text.]]></title>
            <link>https://medium.com/@hitesh4296/server-sent-events-breaking-down-how-chatgpt-streams-text-4b1d2d4db4ce?source=rss-f9240e7b4b01------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b1d2d4db4ce</guid>
            <category><![CDATA[server-sent-events]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Hitesh Narwal]]></dc:creator>
            <pubDate>Thu, 05 Dec 2024 10:58:43 GMT</pubDate>
            <atom:updated>2024-12-05T10:58:43.350Z</atom:updated>
            <content:encoded><![CDATA[<h4>How ChatGPT and Other LLMs Stream Text Responses Over HTTP</h4><p>When you think of chat applications, WebSockets or other bi-directional communication protocols often come to mind. Surprisingly, platforms like ChatGPT don’t rely on these complex frameworks. Instead, they use a simple yet powerful mechanism: <strong>Event Streams over HTTP</strong>, supported even in HTTP/1.1. This is where <strong>Server-Sent Events (SSE)</strong> shine — a uni-directional communication protocol perfect for scenarios like real-time text streaming. Let’s break down how ChatGPT leverages this elegant solution.</p><h3><strong>Server Sent Events</strong></h3><p>Think of Server-Sent Events (SSE) like a one-way street. As you start traveling down this road, you begin to receive updates along the way — like signs or billboards telling you about things happening nearby.</p><p>Similarly, when a client or browser connects to a server using <strong>EventSource</strong> over HTTP, it opens a communication channel. Once this connection is established, the server continuously sends updates to the client whenever something new happens, like how you’d receive new signs as you keep driving down the street.</p><p>It’s a simple, efficient way to get real-time updates without having to constantly ask the server for them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*cFB91AzwY6dM-xojDa99KQ.png" /><figcaption>Image credits; <a href="https://simonprickett.dev/">https://simonprickett.dev/</a></figcaption></figure><p>This makes Server-Sent Events an excellent alternative to WebSockets, especially when the communication is server-intensive and you need fine control over when updates are sent. For example, in scenarios like Large Language Models (LLMs), where the server isn’t doing much action while a prompt is being supplied, SSE works perfectly. The server can just keep sending updates as it processes the request, without the need for a continuous, bi-directional connection like WebSockets. It’s a more efficient and straightforward way to stream data in such use cases.</p><h3>Pros:</h3><ol><li><strong>Real-time updates</strong>: SSE allows for continuous, live updates from the server to the client, making it ideal for real-time communication.</li><li><strong>Server-agnostic</strong>: It works with any server capable of handling HTTP requests and responses, making it a versatile solution.</li><li><strong>Custom event handling</strong>: SSE allows for events to be customised and handled on the client-side, offering flexibility in how data is processed.</li></ol><h3>Cons:</h3><ol><li><strong>Client must stay online</strong>: The client must maintain an active connection to receive updates, making it unsuitable for offline scenarios.</li><li><strong>HTTP 1.1 limitations</strong>: Unlike HTTP 2, which supports multiple simultaneous connections, HTTP 1.1 restricts this to only 6, which can impact performance if multiple streams are needed.</li><li><strong>Overloading the client</strong>: A large number of events can overwhelm the client, especially if the client isn’t optimized for handling continuous streams.</li></ol><h3>General Use Cases:</h3><ol><li><strong>Controlled notification channels</strong>: Web-based in-app notifications, live scores, or real-time commentary.</li><li><strong>Live location updates</strong>: Sharing and updating user location in real-time.</li><li><strong>LLMs and similar chats</strong>: Updating context or responses in real-time for applications like chatbots or interactive AI systems.</li></ol><p>Engineering is understanding the trade-offs, and impactful engineering is making the right trade-offs</p><h3>When Not to Use:</h3><p>SSE may not be ideal for lighter clients where maintaining an event stream (both on the server and client) might add unnecessary overhead.</p><h3><strong>Implementation</strong></h3><p>In this implementation, we’ll set up a basic server using <strong>FastAPI</strong> that streams text data to a client via SSE. We’ll simulate a slow stream by sending text updates at regular intervals, and the client will receive these updates in real-time. This example will lay the foundation for how we can implement SSE in an actual application, such as streaming responses from an LLM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kIjwF-Ishe6fk1uiNw_xpA.png" /><figcaption>Find the code here: <a href="https://github.com/Hitesh4296/python-pancake/blob/main/main.py">https://github.com/Hitesh4296/python-pancake/blob/main/main.py</a></figcaption></figure><p>Here’s a quick breakdown:</p><ol><li><strong>Setting Up the FastAPI App</strong>: We’ll create a FastAPI instance and configure it with CORS middleware to allow requests from any origin.</li><li><strong>Simulating a Slow Stream</strong>: We’ll define an asynchronous generator function that simulates streaming data by sending chunks of text (using async def and yield).</li><li><strong>Sending Events to the Client</strong>: Each chunk will be formatted as an SSE message, with the necessary event, data, and ID fields.</li><li><strong>Handling Completion</strong>: After sending the specified number of events, we’ll send a final “terminate” event to let the client know the streaming is complete.</li></ol><p>Here’s what it looks like on the network tab:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TyBHtNTkPJmN3q2VzYrvIA.png" /></figure><h3><strong>Frontend</strong></h3><p>For the front-end, we’ll set up a basic React application to receive and display real-time text updates from the FastAPI server via SSE. We’ll use React hooks like useState and useEffect to manage the stream and update the UI with new data as it arrives. The connection to the server will remain open, allowing the client to receive updates continuously. This setup demonstrates how SSE can be used to stream real-time data, laying the foundation for applications such as streaming responses from an LLM or other real-time data sources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6BEgC0su7h3cfJXrogBDgg.png" /><figcaption>Code: <a href="https://github.com/Hitesh4296/web-tinkering/blob/main/app/routes/http-streaming.tsx">https://github.com/Hitesh4296/web-tinkering/blob/main/app/routes/http-streaming.tsx</a> Hook: <a href="https://github.com/Hitesh4296/web-tinkering/blob/main/app/hooks/useAnimatedText.tsx">https://github.com/Hitesh4296/web-tinkering/blob/main/app/hooks/useAnimatedText.tsx</a></figcaption></figure><h3><strong>Result</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Niw4UPzC_Bvas1_VtO6YWg.gif" /></figure><h3><strong>Resources</strong></h3><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events">Using server-sent events - Web APIs | MDN</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/EventSource">EventSource - Web APIs | MDN</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b1d2d4db4ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>