<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>WebSocket on AI VOID</title><link>https://ai-blog.noorshomelab.dev/tags/websocket/</link><description>Recent content in WebSocket on AI VOID</description><generator>Hugo</generator><language>en</language><lastBuildDate>Wed, 20 Aug 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://ai-blog.noorshomelab.dev/tags/websocket/index.xml" rel="self" type="application/rss+xml"/><item><title>Chapter 5: Managing Active WebSocket Connections</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-5-connection-manager/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-5-connection-manager/</guid><description>&lt;p&gt;Our current WebSocket endpoint only echoes messages back to the sender. A real chat application needs to handle multiple users, allowing them to join, leave, and send messages that are broadcast to all other connected users (or users in a specific room). This chapter introduces a WebSocket connection manager to address this.&lt;/p&gt;
&lt;h3 id="purpose-of-this-chapter"&gt;Purpose of this Chapter&lt;/h3&gt;
&lt;p&gt;By the end of this chapter, you will:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understand the need for a connection manager in a real-time application.&lt;/li&gt;
&lt;li&gt;Implement a &lt;code&gt;ConnectionManager&lt;/code&gt; class to store and manage active WebSocket connections.&lt;/li&gt;
&lt;li&gt;Modify the WebSocket endpoint to use the manager for connecting, disconnecting, and broadcasting messages.&lt;/li&gt;
&lt;li&gt;Test broadcasting functionality with multiple client connections.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-connection-management"&gt;Concepts Explained: Connection Management&lt;/h3&gt;
&lt;p&gt;When a client connects via WebSocket, the &lt;code&gt;WebSocket&lt;/code&gt; object represents that specific connection. To send a message to all active clients, or a subset of them, our server needs a way to keep track of these individual &lt;code&gt;WebSocket&lt;/code&gt; objects. A &lt;code&gt;ConnectionManager&lt;/code&gt; class typically serves this purpose.&lt;/p&gt;</description></item><item><title>Chapter 7: Enhancing Chat Functionality: Room-based Messaging</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-7-chat-rooms/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-7-chat-rooms/</guid><description>&lt;p&gt;A single global chat channel is fun, but real-world chat applications typically feature multiple &amp;ldquo;rooms&amp;rdquo; or &amp;ldquo;channels&amp;rdquo; where users can have separate conversations. This chapter will modify our &lt;code&gt;ConnectionManager&lt;/code&gt; and WebSocket endpoint to support room-based messaging.&lt;/p&gt;
&lt;h3 id="purpose-of-this-chapter"&gt;Purpose of this Chapter&lt;/h3&gt;
&lt;p&gt;By the end of this chapter, you will:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Modify the &lt;code&gt;ConnectionManager&lt;/code&gt; to manage connections per room.&lt;/li&gt;
&lt;li&gt;Update the WebSocket endpoint to allow clients to specify a chat room.&lt;/li&gt;
&lt;li&gt;Implement broadcasting messages only to users within the same room.&lt;/li&gt;
&lt;li&gt;Create an endpoint to list available rooms.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-room-based-messaging"&gt;Concepts Explained: Room-Based Messaging&lt;/h3&gt;
&lt;p&gt;Instead of a flat list of all active WebSocket connections, we&amp;rsquo;ll use a dictionary where keys are room names (e.g., &amp;ldquo;general&amp;rdquo;, &amp;ldquo;python-dev&amp;rdquo;, &amp;ldquo;random&amp;rdquo;) and values are lists of &lt;code&gt;WebSocket&lt;/code&gt; objects for users in that specific room.&lt;/p&gt;</description></item></channel></rss>