Frontend: One-on-one (Duologue) chatting application with Django channels and SvelteKit
John Owolabi Idogun
Posted on January 7, 2023
Introduction
This is the second part of a series of tutorials on building a one-on-one (duologue) chatting application with Django channels and SvelteKit. We will focus on building the app's frontend in this part.
NOTE: I won't delve much into the nitty-gritty of SvelteKit as I only intend to show how one can interact with WebSocket in the browser in SvelteKit. I wrote some other tutorials that talk more about it.
Full-stack private chatting application built using Django, Django Channels, and SvelteKit
chatting
chatting is a full-stack private chatting application which uses modern technologies such as Python — Django and Django channels — and TypeScript/JavaScript — SvelteKit. Its real-time feature utilizes WebSocket.
recording.mp4
chatting has backend and frontend directories. Contrary to its name, backend is a classic full-fledged application, not only backend code. Though not refined yet, you can chat and enjoy real-time conversations there as well. frontend does what it implies. It houses all user-facing codes, written using SvelteKit and TypeScript.
Run locally
To locally run the app, clone this repository and then open two terminals. In one terminal, change directory to backend and in the other, to frontend. For the frontend terminal, you can run the development server using npm run dev:
╭─[Johns-MacBook-Pro] as sirneij in~/Documents/Devs/chatting/frontend using node v18.11.0 21:37:36
╰──➤ npm run dev
In the backend terminal, create and activate a virtual…
In our chatting folder, create a SvelteKit project by issuing the following command in your terminal:
╭─ sirneij in ~/Documents/Devs/chatting on (main)
╰─(ノ˚Д˚)ノ npm create svelte@latest frontend
From the prompts, I chose a skeleton project and added TypeScript support. Follow the instructions given by the command after your project has successfully been created. I added bootstrap (v5) and fontawesome (v6.2.0) to the frontend. I also added routes/+layout.svelte and modified routes/+page.svelte. routes/chat/[username]/+page.svelte was created as well to house the WebSocket logic. Before then, lib/store/message.store.ts has the following content:
This is a custom writable store that exposes a function sendMessage which does exactly what its name implies. It was used in routes/chat/[username]/+page.svelte to send messages to the backend. Let's look at the content of routes/chat/[username]/+page.svelte:
handleSendMessage gets fired whenever a user sends a message. The only thing it does is use the exposed sendMessage function to send the message to the backend. sendMessage takes, among others, the WebSocket. It was initialized with let socket: WebSocket; which was populated with:
It must be in the browser block since SvelteKit does Server-side rendering by default which can be turned off but since we ain't turning it off, we must ensure WebSocket is initialized only in the browser since it's a browser-based API.
With this, we are done! Ensure you take a look at the complete code on GitHub.
Outro
Enjoyed this article? I'm a Software Engineer and Technical Writer actively seeking new opportunities, particularly in areas related to web security, finance, healthcare, and education. If you think my expertise aligns with your team's needs, let's chat! You can find me on LinkedIn and Twitter.
If you found this article valuable, consider sharing it with your network to help spread the knowledge!