<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Chapters for Building a Real-time Chat Application on AI VOID</title><link>https://ai-blog.noorshomelab.dev/chat-guide/</link><description>Recent content in Chapters for Building a Real-time Chat Application 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/chat-guide/index.xml" rel="self" type="application/rss+xml"/><item><title>Chapter 1: Initial Project Setup and Virtual Environment</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-1-initial-setup/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-1-initial-setup/</guid><description>&lt;p&gt;The first step in any robust software project is to set up a clean and isolated development environment. This chapter will guide you through creating a new project directory and establishing a Python virtual environment to manage dependencies effectively.&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 have:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A dedicated project folder for your chat application.&lt;/li&gt;
&lt;li&gt;A Python virtual environment configured to keep project dependencies separate from your system-wide Python installation.&lt;/li&gt;
&lt;li&gt;Installed FastAPI and Uvicorn, the core components of our application.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-virtual-environments"&gt;Concepts Explained: Virtual Environments&lt;/h3&gt;
&lt;p&gt;A virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.&lt;/p&gt;</description></item><item><title>Chapter 10: Securing WebSocket Communication</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-10-websocket-security/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-10-websocket-security/</guid><description>&lt;p&gt;So far, our chat application uses JWT for HTTP authentication and passes the token as a query parameter for WebSockets. While this identifies the user, the actual WebSocket data transfer is currently unencrypted (WS://). For production, all traffic, especially sensitive chat messages, &lt;strong&gt;must&lt;/strong&gt; be encrypted using WSS (WebSocket Secure), which relies on TLS/SSL certificates. This chapter focuses on enabling WSS and reinforcing WebSocket authentication.&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;</description></item><item><title>Chapter 11: Dockerizing Your FastAPI Chat Application</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-11-dockerization/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-11-dockerization/</guid><description>&lt;p&gt;As our application grows, ensuring a consistent development environment and simplifying deployment becomes critical. Docker provides &lt;strong&gt;containerization&lt;/strong&gt;, packaging your application and all its dependencies into a single, isolated unit called a container. This chapter will guide you through Dockerizing our FastAPI chat application.&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 benefits of Docker for development and deployment.&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;Dockerfile&lt;/code&gt; to build a Docker image for our application.&lt;/li&gt;
&lt;li&gt;Use Docker Compose to run the application along with a database (optional, for real DB).&lt;/li&gt;
&lt;li&gt;Run your FastAPI chat application inside a Docker container.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-docker-and-dockerfile"&gt;Concepts Explained: Docker and Dockerfile&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Docker&lt;/strong&gt; is a platform that uses OS-level virtualization to deliver software in packages called containers. Containers are isolated from each other and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels.&lt;/p&gt;</description></item><item><title>Chapter 12: Deployment Strategies and Considerations</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-12-deployment/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-12-deployment/</guid><description>&lt;p&gt;You&amp;rsquo;ve built a real-time chat application, complete with authentication, rooms, message persistence, and Dockerization. Now, the final frontier is deploying it to a production environment. This chapter discusses various deployment strategies and crucial considerations for making your application scalable, reliable, and secure in the wild.&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 role of Gunicorn and reverse proxies in FastAPI deployments.&lt;/li&gt;
&lt;li&gt;Be familiar with essential production configurations (environment variables, logging).&lt;/li&gt;
&lt;li&gt;Learn about common deployment platforms (PaaS, VMs, Kubernetes).&lt;/li&gt;
&lt;li&gt;Grasp key security and scalability considerations for a production environment.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-production-deployment-stack"&gt;Concepts Explained: Production Deployment Stack&lt;/h3&gt;
&lt;p&gt;For local development, running &lt;code&gt;uvicorn app.main:app --reload&lt;/code&gt; is fine. However, in production, Uvicorn is typically used as a worker within a more robust ASGI server like &lt;strong&gt;Gunicorn&lt;/strong&gt;, and often fronted by a &lt;strong&gt;reverse proxy&lt;/strong&gt; like Nginx or Caddy.&lt;/p&gt;</description></item><item><title>Chapter 2: Your First FastAPI Endpoint and Project Structure</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-2-fastapi-intro/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-2-fastapi-intro/</guid><description>&lt;p&gt;With your development environment ready, it&amp;rsquo;s time to write some code! This chapter focuses on creating a minimal FastAPI application, understanding its basic components, and establishing a sensible project structure.&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 be able to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Write a simple &amp;ldquo;Hello, World!&amp;rdquo; FastAPI application.&lt;/li&gt;
&lt;li&gt;Run the FastAPI application using Uvicorn.&lt;/li&gt;
&lt;li&gt;Understand basic FastAPI routing.&lt;/li&gt;
&lt;li&gt;Create a foundational project directory structure.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-fastapi-basics"&gt;Concepts Explained: FastAPI Basics&lt;/h3&gt;
&lt;p&gt;FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.8+ based on standard Python type hints. It offers:&lt;/p&gt;</description></item><item><title>Chapter 3: Introducing WebSockets - Real-time Foundations</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-3-websockets-intro/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-3-websockets-intro/</guid><description>&lt;p&gt;The core of any real-time chat application is its ability to establish persistent, bidirectional communication channels. This is where WebSockets come in. In this chapter, we&amp;rsquo;ll integrate a basic WebSocket endpoint into our FastAPI application.&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 be able to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understand the fundamental concept of WebSockets.&lt;/li&gt;
&lt;li&gt;Implement a FastAPI WebSocket endpoint.&lt;/li&gt;
&lt;li&gt;Send and receive messages over a WebSocket connection.&lt;/li&gt;
&lt;li&gt;Test your WebSocket connection using a simple client.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-websockets"&gt;Concepts Explained: WebSockets&lt;/h3&gt;
&lt;p&gt;Traditionally, HTTP is a request-response protocol: a client sends a request, the server sends a response, and the connection closes. This isn&amp;rsquo;t ideal for real-time applications where information needs to be pushed from the server to the client instantly, and vice-versa, without constant polling.&lt;/p&gt;</description></item><item><title>Chapter 4: Basic User Authentication with JWT</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-4-basic-auth/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-4-basic-auth/</guid><description>&lt;p&gt;Before users can chat, we need to know who they are. This chapter focuses on implementing a basic user authentication system using JSON Web Tokens (JWT) in FastAPI. JWTs are a common, secure way to transmit information between parties as a JSON object, ideal for stateless authentication in APIs.&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 what JWTs are and why they are used for authentication.&lt;/li&gt;
&lt;li&gt;Set up libraries for password hashing and JWT generation.&lt;/li&gt;
&lt;li&gt;Implement user creation and login endpoints.&lt;/li&gt;
&lt;li&gt;Create a dependency to protect FastAPI routes with JWT.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-jwt-and-hashing"&gt;Concepts Explained: JWT and Hashing&lt;/h3&gt;
&lt;h4 id="json-web-tokens-jwt"&gt;JSON Web Tokens (JWT)&lt;/h4&gt;
&lt;p&gt;A JWT is a compact, URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object and are digitally signed using a secret (with HMAC algorithm) or a public/private key pair (with RSA or ECDSA).&lt;/p&gt;</description></item><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 6: Storing Messages with SQLite</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-6-sqlite-messages/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-6-sqlite-messages/</guid><description>&lt;p&gt;A real chat application needs to store messages to provide chat history. This chapter will guide you through setting up a SQLite database and integrating it into our FastAPI application using SQLAlchemy, a powerful SQL toolkit and Object-Relational Mapper (ORM).&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 basics of ORM and why we use SQLAlchemy.&lt;/li&gt;
&lt;li&gt;Set up a SQLite database connection.&lt;/li&gt;
&lt;li&gt;Define database models for users and chat messages.&lt;/li&gt;
&lt;li&gt;Implement methods to store new messages and retrieve chat history.&lt;/li&gt;
&lt;li&gt;Update the WebSocket endpoint to save messages.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-sqlalchemy-and-orm"&gt;Concepts Explained: SQLAlchemy and ORM&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Object-Relational Mapping (ORM)&lt;/strong&gt; is a technique that lets you query and manipulate data from a database using an object-oriented paradigm. Instead of writing raw SQL, you interact with database tables as Python classes and objects.&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><item><title>Chapter 8: User Registration and Login Flow</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-8-user-auth-flow/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-8-user-auth-flow/</guid><description>&lt;p&gt;While we introduced basic user registration and login in Chapter 4, this chapter focuses on refining these critical endpoints. We&amp;rsquo;ll ensure robust data validation, provide clear error messages, and integrate the user management more tightly with our database. This is about building a solid, production-ready authentication foundation.&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;Have dedicated endpoints for user registration and login.&lt;/li&gt;
&lt;li&gt;Understand data validation for incoming user data.&lt;/li&gt;
&lt;li&gt;Implement proper password hashing and verification.&lt;/li&gt;
&lt;li&gt;Ensure that authenticated users are correctly identified and used throughout the application.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-data-validation--error-handling"&gt;Concepts Explained: Data Validation &amp;amp; Error Handling&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Data Validation:&lt;/strong&gt; Ensuring that incoming data (like username and password during registration) meets expected criteria. FastAPI leverages Pydantic for this, which allows you to define strict schemas for your request bodies.&lt;/p&gt;</description></item><item><title>Chapter 9: Advanced Error Handling and Logging</title><link>https://ai-blog.noorshomelab.dev/chat-guide/chapter-9-error-logging/</link><pubDate>Wed, 20 Aug 2025 00:00:00 +0000</pubDate><guid>https://ai-blog.noorshomelab.dev/chat-guide/chapter-9-error-logging/</guid><description>&lt;p&gt;As applications grow and move into production, robust error handling and comprehensive logging become indispensable. This chapter focuses on setting up structured logging, handling custom exceptions, and providing graceful error responses in our FastAPI chat application.&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;Configure Python&amp;rsquo;s &lt;code&gt;logging&lt;/code&gt; module for structured output.&lt;/li&gt;
&lt;li&gt;Implement custom exception handlers for specific application errors.&lt;/li&gt;
&lt;li&gt;Ensure that unhandled exceptions are caught and logged appropriately.&lt;/li&gt;
&lt;li&gt;Understand best practices for logging sensitive information.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="concepts-explained-structured-logging--custom-exception-handling"&gt;Concepts Explained: Structured Logging &amp;amp; Custom Exception Handling&lt;/h3&gt;
&lt;h4 id="structured-logging"&gt;Structured Logging&lt;/h4&gt;
&lt;p&gt;Traditional logging often outputs plain text messages. &lt;strong&gt;Structured logging&lt;/strong&gt; outputs logs in a consistent, machine-readable format, typically JSON. This makes logs much easier to parse, filter, and analyze with log management tools (e.g., ELK Stack, Splunk, DataDog).&lt;/p&gt;</description></item></channel></rss>