<?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"><channel><title><![CDATA[Preet Blog]]></title><description><![CDATA[Preet Blog]]></description><link>https://preet.click</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Apr 2026 11:39:56 GMT</lastBuildDate><atom:link href="https://preet.click/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[A Beginner's Guide to Server-Sent Events (SSE)]]></title><description><![CDATA[Server-Sent Events (SSE) is a web standard that lets a server push data to a browser in real-time over a single, long-lived HTTP connection. Think of it as a one-way communication channel where the server can continuously send updates to the client w...]]></description><link>https://preet.click/a-beginners-guide-to-server-sent-events-sse</link><guid isPermaLink="true">https://preet.click/a-beginners-guide-to-server-sent-events-sse</guid><category><![CDATA[General Programming]]></category><category><![CDATA[Web API]]></category><dc:creator><![CDATA[Preet Mungara]]></dc:creator><pubDate>Mon, 25 Aug 2025 02:49:34 GMT</pubDate><content:encoded><![CDATA[<p>Server-Sent Events (SSE) is a web standard that lets a server push data to a browser in real-time over a single, long-lived HTTP connection. Think of it as a one-way communication channel where the server can continuously send updates to the client without the client having to repeatedly ask for them.</p>
<p>How ChatGPT uses Server-Sent Events to give answer of one prompt.</p>
<ul>
<li><p>You <strong>write one prompt.</strong></p>
</li>
<li><p>Server keeps pushing updates to you as they happen from model.</p>
</li>
</ul>
<p>Here i’ve asked one simple question.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750498386168/46f230b7-547d-4d10-a6de-10bb5d30b58d.png" alt="Simple ChatGPT Prompt." class="image--center mx-auto" /></p>
<p>Inspect the request that we’ve sent.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750498447856/9c5027ea-864b-417c-8d93-a5a1e8ce4731.png" alt="ChatGPT Request." class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750498507994/7b95e881-676d-4234-bcb4-7990ff542bca.png" alt="ChatGPT Request Header." class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750498549135/a704dc9e-79d2-43be-916e-36554331bf20.png" alt="ChatGPT Request Header where our prompt is being passed.." class="image--center mx-auto" /></p>
<p>Now, we can see the EventStream result. It keeps pushing data to the connection as it receives it. Look at the time, many metadata-related responses and the actual response are being sent.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750498632478/7ca812bb-7098-4f2d-a5b6-0e07bb286096.png" alt="ChatGPT Response of EventStream where answer is given with other metadata." class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750498730440/af463a56-17c0-4f74-a0d3-d6b418ee5dda.png" alt="ChatGPT Response of EventStream where answer is given with other metadata." class="image--center mx-auto" /></p>
<h2 id="heading-whats-the-difference-between-polling-websocket-and-server-sent-events">What's the difference between Polling, WebSocket and Server-Sent Events?</h2>
<p>Polling is about client sending requests to the server at regular intervals to check if there are any new updates. sometimes there is no new data at server side, resulting in unnecessary request.</p>
<p>WebSocket is generally suitable for bidirectional communication on single active connection, for example: collaboration tools, real time polling app.</p>
<p>Server-Sent Event is lightweight, one directional communication from server to client over HTTP, it’s unidirectional only.</p>
<h2 id="heading-example">Example:</h2>
<p>Here is simple SSE Example, the API endpoint gives the current Date &amp; Time in response at every second.</p>
<pre><code class="lang-csharp">app.MapGet(<span class="hljs-string">"/stream-time"</span>, <span class="hljs-keyword">async</span> (HttpContext context) =&gt;
{
    <span class="hljs-keyword">var</span> response = context.Response;
    response.Headers.Append(<span class="hljs-string">"Content-Type"</span>, <span class="hljs-string">"text/event-stream"</span>);
    response.Headers.Append(<span class="hljs-string">"Cache-Control"</span>, <span class="hljs-string">"no-cache"</span>);
    response.Headers.Append(<span class="hljs-string">"Connection"</span>, <span class="hljs-string">"keep-alive"</span>);

    <span class="hljs-keyword">while</span> (!context.RequestAborted.IsCancellationRequested)
    {
        <span class="hljs-keyword">var</span> message = <span class="hljs-string">$"The time on the server is: <span class="hljs-subst">{DateTime.Now:T}</span>"</span>;
        <span class="hljs-keyword">await</span> response.WriteAsync(<span class="hljs-string">$"data: <span class="hljs-subst">{message}</span>\n\n"</span>);
        <span class="hljs-keyword">await</span> response.Body.FlushAsync();
        <span class="hljs-keyword">await</span> Task.Delay(<span class="hljs-number">1000</span>);
    }
});
</code></pre>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Minimal SSE Demo<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Server-Sent Events Demo<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>Connection Status:<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"status"</span>&gt;</span>Connecting...<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>Latest Server Message:<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"live-data"</span>&gt;</span>
        Waiting for data...
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
        <span class="hljs-keyword">const</span> statusElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'status'</span>);
        <span class="hljs-keyword">const</span> dataElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'live-data'</span>);

        <span class="hljs-keyword">const</span> SSE_ENDPOINT = <span class="hljs-string">'http://localhost:5139/stream-time'</span>;

        <span class="hljs-comment">// This is the key line. Browser provides EventSource API to implement SSE.</span>
        <span class="hljs-keyword">const</span> eventSource = <span class="hljs-keyword">new</span> EventSource(SSE_ENDPOINT);

        eventSource.onopen = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Connection to server opened."</span>);
            statusElement.textContent = <span class="hljs-string">'Connected'</span>;
        };

        eventSource.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) </span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Received data:"</span>, event.data);
            dataElement.textContent = event.data;
        };

        eventSource.onerror = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
            <span class="hljs-built_in">console</span>.error(<span class="hljs-string">"EventSource failed:"</span>, error);
            statusElement.textContent = <span class="hljs-string">'Error / Disconnected'</span>;
        };

    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>So when we load the page, API get’s hit and Single HTTP Connection gets established which gives date and time and on UI we get date and time.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750509591802/7cf88dd2-bc35-42dd-80f5-c8a2d633ffea.png" alt="SSE Request EventSteam." /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750509607336/68f69707-d7a1-4081-b0ce-7abc9960e4de.png" alt="Response of SSE API." /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750510334778/0a2ac868-29ae-46bc-9c4f-acec9963b8de.png" alt="Log of Response of SSE API." class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750509742916/a80c91c5-4a9e-41cf-8aee-7d654d54bc6f.gif" alt="Front End showing data that we received from API." /></p>
<p>Now you might realize how ChatGPT and other AI applications use the same concept of Server-Sent Events to take advantage of this browser feature for their needs, instead of choosing WebSocket, which might be too complex for the same purpose.</p>
<p>For more understanding and real-world use cases, you can refer to the list of blogs below.</p>
<p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events">https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events</a><br /><a target="_blank" href="https://shopify.engineering/server-sent-events-data-streaming">https://shopify.engineering/server-sent-events-data-streaming</a><br /><a target="_blank" href="https://germano.dev/sse-websockets">https://germano.dev/sse-websockets</a></p>
]]></content:encoded></item><item><title><![CDATA[New Experiment.]]></title><description><![CDATA[December 1, 2024! With only a few weeks left until the year ends and 2025 begins, I've decided to tackle my New Year's resolution ahead of time. After countless bouts of procrastination and endless excuses, it's time to finally take action. let’s see...]]></description><link>https://preet.click/new-experiment</link><guid isPermaLink="true">https://preet.click/new-experiment</guid><category><![CDATA[writing]]></category><category><![CDATA[first post]]></category><dc:creator><![CDATA[Preet Mungara]]></dc:creator><pubDate>Sun, 01 Dec 2024 08:58:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1733043782135/e6b0665a-db96-480a-9644-abcf52d06277.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>December 1, 2024! With only a few weeks left until the year ends and 2025 begins, I've decided to tackle my New Year's resolution ahead of time. After countless bouts of procrastination and endless excuses, it's time to finally take action. let’s see how this experiment goes.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733043212358/12d949d0-8603-4a13-828f-6b6527222b9c.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item></channel></rss>