Hey there, and welcome back to "Git Checkout"!
You might remember, when I first started this newsletter journey, I had started learning the Rust programming language. My very first piece was about a GitHub repo showcasing a news reader app built in Rust. Last time, I mused over the idea of a ChatGPT tailored for financial reports. This time around, I'm mixing things up a bit: I'm going to talk about crafting a ChatGPT clone using Rust. Now, I'm still finding my way around Rust, so I haven't fully examined the entire code base yet. But I want to share some neat bits and pieces that really stood out to me.
So, without further ado, let's dive into this issue where we'll explore:
- The GitHub repository,
- A quote about Rust I stumbled upon.
Excited to share this with you – let's get started!
The GitHub repository
This repo I am about to talk about is a cool demonstration of how to set up a web server, implement a websocket, and do some neat conditional rendering in Rust. There's a lot more under the hood, but for now, let's just focus on these parts. I'm still getting the hang of Rust, so I want to keep things simple and digestible – nobody wants to wade through a super long article, right?
Oh, and guess what? The creator of this repo has a YouTube channel where they showcase it all. You should definitely check it out here.
As always, I've done my best to break down the key parts of the code into easy-to-understand bits. I'll also highlight some areas where you could jump in – maybe even make your first contribution to an open-source project.
So, let’s get started:
The repo: rusty-llama
Web Server: The project uses Actix Web, a framework for building web applications in Rust. It sets up an HttpServer, which is the starting point of the web application. The server is configured to handle different types of requests through routes. For instance, there's a route for /ws which deals with WebSocket connections, and another route /api for handling API requests. The code demonstrates how to add functionalities (middlewares and services) to the server, necessary for processing web requests and responses.
Websocket: The WebSocket part of the project is about enabling real-time communication between the server and the client (like a user's browser). A specific route (/ws) is designated in the server to handle incoming WebSocket requests. When a request is received on this route, a function ‘ws’ initiates a WebSocket session allowing back and forth communication between the server and the client in real-time.
Conditional Rendering: This refers to the application's ability to run different sets of instructions based on certain conditions. In this case, it's about choosing between server-side rendering (SSR) and client-side rendering (CSR). Rust's cfg_if! macro is used to compile different versions of the main function depending on whether SSR or CSR is enabled. This allows the same code base to be used differently by toggling features during the compilation process.
I hope this clear and straightforward explanation helps you understand the functionalities of the project better!
Probable areas of contribution:
I am not trying to make this just like ChatGPT, but here are a couple of ideas to improve the app for learning:
Chat Summarization: How about adding a new feature that gives users a short summary of their long chat conversations? This could be handy for remembering what was talked about before.
We could keep a record of each chat - either just while the chat is happening or save it in a database for looking back later. When a user wants a summary, or when the chat ends, the app could take the chat history and create a short version of it. Users could see this summary by either asking for it with a command like “Show summary” or automatically get it when they finish chatting.
Integrating with External Services: Another cool feature could be to make the chatbot do more things by connecting it with outside services – like checking the weather, getting news updates, or even shopping.
We could use Rust’s tools for making web requests to get data from these services. Mostly, the data would be fetched in a format called JSON, which would then be sorted through to find and use the needed bits.
A quote about Rust
"Rust represents what we've learned over the past few decades about what works well for larger programs written by teams of people, particularly in environments where they're not all super experienced with the system." – Graydon Hoare, the creator of Rust
This quote reflects on the evolution and design philosophy behind Rust, emphasising its focus on enabling collaborative, robust, and safe software development.
That's a wrap for today's issue. As always, I value your thoughts and ideas! Do you have a specific repository or topic in mind that you're eager to delve into? I'm here to listen!
I want to make this newsletter a shared adventure. Your suggestions are more than welcome and might just be the focus of our next discussion.
If you liked what you read, please do not hesitate to share the love.
Until we meet again,
Cheers,
– Krish
This is a great find, thanks for the writeup. If you ever find something a project that uses websockets and also persists data to a MongoDB db, I would love to see that.
Lol, and if the project is in React or NextJS/Node... it would be perfections...
Working Websocket examples are rare...