---
title: "Baithak: Building Real-Time Team Chat with an Issue Tracker"
description: "How I built Baithak on my own in two weeks: Django Channels and React team chat with threads, direct messages, encrypted and virus-scanned files, search and an issue tracker linked to the conversation, tested by 1,443 backend tests, two browsers in one channel and fifteen simulated attackers."
author: "Syed Ahmer Shah"
date: 2026-11-29
url: https://ahmershah.dev/blogs/baithak-case-study-real-time-team-chat
tags: ["baithak", "case-study", "django", "websockets", "react", "mysql", "security", "system-design"]
series: "The Engineering Logs" (part 25)
---

# Baithak: Building Real-Time Team Chat with an Issue Tracker

_A case study on the parts of a chat app that aren't the message box: order, access, files, sign-in, and proving all of it under load_

![Baithak's #dev channel with a message, a teammate's reply and its thread open, under the title Baithak: real-time team chat with an issue tracker](https://ahmershah.dev/blog/baithak-case-study-real-time-team-chat/cover-9442ac06.webp)


Small teams usually keep two tabs open all day. One is chat, where the work is actually discussed: the bug someone spotted, the decision about the deploy, the screenshot that explains everything. The other is the tracker, where the work is supposed to live. The link between the two is a copy and paste that someone forgets, and three weeks later nobody can find out why an issue was closed.

Baithak (بیٹھک, the room where people sit together and talk) is my answer to that. It is team chat with channels, threads, direct messages, files and search, and a small issue tracker where any message can become an issue and the issue's discussion stays in that message's thread. GitHub pull request and issue events arrive in the channel that follows the repository. I built it myself between 15 and 29 November 2026, across 70 pull requests and seven pre-releases.

The message box is the easy part of a chat app. This is about the rest.

* * *

## The rules came before the code

Before the first model existed, I wrote down the rules the code would have to keep. They lived in the repository's agent guide and every pull request was reviewed against them:

- **Messages are saved before they are broadcast.** The socket is a delivery mechanism, never the record.
- **A channel you can't see answers 404, never 403.** Existence is information too.
- **One function decides who can read a channel.** The REST API, the sockets, search and notifications all ask it.
- **Edits and deletes keep the row.** A deleted message loses its text, not its place, so threads and counts still add up.
- **Uploaded files are scanned before anyone can download them,** encrypted at rest and served through expiring links.
- **Every change to workspace data writes an audit row** in the same transaction.

Writing them first changed how I built everything after. When a feature tempted me to cut a corner, the rule was already on the page, and so was the test that would catch it.

![The #dev channel with Amna's message, Bilal's reply and the thread open beside it](/blog/baithak-case-study-real-time-team-chat/a4ac402c62.webp)

## Saved before sent

Every channel row carries a counter, `last_seq`. Posting a message locks that row, takes the next number, saves the message and its mentions, and commits. Only after the commit does the message go out to the channel's group on the Redis channel layer, and from there to every open tab.

That ordering buys one property I cared about more than any other: **every client sees the same order, and a client that missed something can always ask for it.** If a tab sees message 41 and then 43, it knows exactly what to ask the API for. After a reconnect it asks for everything after the last change it saw.

There's a second counter, `last_event`, because order of arrival and order of change are different things. A message keeps its `seq` forever, but an edit, a delete or a new reaction bumps its `event`. Catch-up asks for `after_event`, so an edit made while your laptop was asleep reaches you too, not just new messages.

The broadcast happens in `transaction.on_commit`, so a rolled-back transaction never tells anyone about a message that doesn't exist. A message is never "sent" and then quietly missing from history.

## One door for access

Access bugs in chat apps rarely come from the main path. They come from the fourth path somebody added later: search, notifications, a file link, the socket that was already subscribed before you were removed.

So there's exactly one rule for who can read a channel, written in plain Python in `apps/core` with no Django imports: role, channel kind and membership in, yes or no out. It has an SQL twin for listing channels, and a test compares the two for every combination of role, kind and membership so they can never drift.

Everything uses it. The REST API, the WebSocket consumer, search, notifications, file downloads. When your access changes, your open socket recomputes its subscriptions without reconnecting, so being removed from a private channel stops the next message, not the next page load.

A test lists every workspace endpoint in the project and fails if a new one isn't on the list. Each listed endpoint is called as an outsider and must answer 404, as a signed-out visitor and must answer 403, and with an unknown workspace slug and must answer 404. Adding an endpoint without thinking about access is a red build, not a code review comment.

## Threads, unread counts and issues

Threads are one level deep, by choice. Nested threads look powerful and read badly. A reply can also be sent to the channel, and the parent shows its count and last reply time.

Unread counts are a marker that only moves forward: a conditional update that refuses to go back. Read something on your phone and your laptop agrees within a second, and a stale tab can never make old messages unread again.

The issue tracker is deliberately small: projects with keys, labels, priorities, assignees, due dates and a board. Issue numbers are unique per project and handed out under the project row's lock, so there are no gaps and no duplicates even when two people file at once. The part I like most is the link to chat. Any message can become an issue, the message shows the issue's key, and the issue page shows that message's thread as its discussion. Changes to the issue are posted back into the thread as activity lines, so the conversation and the work never separate.

![The Bazaar app board, with the new issue in To do](/blog/baithak-case-study-real-time-team-chat/c27cb824b4.webp)

## Files: checked, scanned, encrypted

Files are where a chat app turns into an attack surface. Every upload goes through four gates:

1. **Checked by content, not name.** The type is sniffed from the first 4 KB. A program renamed `photo.png` is refused at upload.
2. **Inspected in full.** Images must decode, PDFs can't carry scripts or launch actions, zips can't be password-protected, hold macros or unpack to absurd sizes.
3. **Scanned by ClamAV** on its own Celery queue, so a burst of uploads never holds up email.
4. **Encrypted at rest** with AES-256-GCM in 64 KiB chunks, with key ids so keys can rotate.

Until a file is clean, nobody can download it, the uploader included. The message shows "Checking for viruses", and a refused file loses its bytes but keeps its name and the reason, so the conversation still makes sense. Downloads go through signed links that expire after 12 hours and check access again. Image previews are made only for clean files, with camera and location data stripped.

## Sign-in without a weak door

Passwords are Argon2id, with rules strict enough to matter. Wrong passwords, reset codes and two-step codes all climb the same lockout ladder: an hour, four hours, a week, then the account is closed to sign-in and the owner is emailed at every step. Reset codes are six random digits, stored only as a keyed hash, valid once for ten minutes, with three tries.

Two-step sign-in follows RFC 6238, written in `apps/core` against the RFC's own test vectors. Each code works once, and there are ten hashed recovery codes. Settings lists every signed-in device, and signing one out closes its open sockets immediately.

Continue with Google uses the OpenID Connect code flow with PKCE, state and nonce, and checks the ID token's signature, issuer, audience and expiry. Workspaces can also connect their own provider, such as Okta, Entra ID or Keycloak, and require it. That provider is trusted less than Google: its email never links to or takes over an existing account, because the workspace owner runs it and could type any address into it. New people prove their address with an emailed code first.

The Django admin was the last weak door. It had its own password form with none of the above, so it now sends people to the app's sign-in and only lets in staff whose session passed a two-step code.

![Your settings: email notifications, quiet hours, two-step sign-in and signed-in devices](/blog/baithak-case-study-real-time-team-chat/a05965c82a.webp)

## Design: a warm room, not a dashboard

I wanted Baithak to feel like the name: a room, not a control panel. The palette is warm cream and clay with six label hues, Fraunces for display type and Instrument Sans for everything else, and every color is a semantic token with a light and a dark value. A small script checks every text and background pair against WCAG 2.2 AA in both themes and fails CI if one slips.

Messages use Discord-style Markdown: bold, italics, strikethrough, spoilers, code, quotes and lists. It is parsed on the client into React elements, so no user text ever becomes HTML, and `dangerouslySetInnerHTML` appears nowhere in the codebase. Links are only `http` and `https`.

## SEO for an app that is mostly private

Almost all of Baithak sits behind sign-in, and that shapes the search strategy. Only four pages are indexed: sign-up, sign-in, privacy and contact. Each one sets its own title under 60 characters, a description under 140, a canonical link and Open Graph tags. Every other route is `noindex`. `robots.txt` keeps crawlers away from workspaces and from links that carry one-time tokens. `llms.txt` and `llms-full.txt` describe the product in plain sentences for AI crawlers. The JSON-LD names the app and its author and nothing it can't back up.

## Proving it

I didn't want to claim any of this. I wanted to show it.

- **1,443 backend tests with 100% line and branch coverage,** gated in CI. Getting from 93% to 100% turned up dead code and a check that could never fire, both now deleted.
- **220 frontend tests,** and **9 end-to-end tests** in real Chromium against the production build. They include two people in one channel: messages, edits and deletes reaching the other browser live, threads and reactions, and a browser that went offline catching up on what it missed.
- **A swarm of fifteen simulated people** against a backend with every real limit on, each from their own address. Five chat normally while the others flood messages, open 25 sockets, upload broken images, a virus test file, a PDF with scripts, a zip bomb and a disguised program, race four sign-ups for one handle, guess passwords and reset codes, probe a private channel and someone else's workspace, and hammer search, webhooks and the contact form. All 32 checks pass.
- **A stress test** with 300 live sockets in one channel delivered all 6,000 messages with a p95 of 483 ms on a single server process.

![Search, showing only what Amna is allowed to read](/blog/baithak-case-study-real-time-team-chat/89c3e10909.webp)

## Where it was hard

**Idle sockets dropped every few seconds.** Redis read timeouts were five seconds, exactly as long as Channels' blocking wait for the next event, so every quiet socket crashed and reconnected. The fix was a single setting. Finding it took an afternoon of watching reconnect logs.

**Join announcements grow with the square of the crowd.** Five hundred people connecting means every one of them hears about every other one. The first load check dropped most announcements. Batching them per workspace on a short timer fixed it, and the load check now checks that every client ends up with the right online list.

**An edit could resurrect a deleted message.** An edit waiting on the channel lock could write its text back into a message deleted while it waited. Every edit and reaction now checks the message again under the lock.

**The swarm found a stuck message.** If the virus scan finished before you pressed send, the server refused the message because its file had been refused, and the message sat on "Not sent. Retry" forever with no reason. It now sends and shows why the file was removed, the same as a file refused after sending.

**My laptop couldn't run three browsers at once.** End-to-end tests in parallel starved a four-core machine until sign-up requests timed out. They run one at a time locally and two at a time in CI.

## What I got wrong

**I trusted a summary over the code.** The status notes said oversized socket frames close the connection. The code drops them unread, which is what the architecture doc said all along. I found it only when the swarm hung waiting for a close that never came. I fixed the notes, not the code.

**I accepted one imperfection.** Fire 160 messages at the exact same moment and the 120-a-minute limit lets 123 through, because the framework's throttle reads and writes without a lock. Three extra messages under a deliberate burst isn't worth a custom limiter. It's written down as a known limitation rather than hidden.

## By the numbers

- 70 pull requests and seven pre-releases in two weeks
- 1,443 backend tests at 100% coverage, 220 frontend tests and 9 end-to-end tests
- 32 swarm checks across 15 simulated people, all passing
- 300 live sockets at a p95 of 483 ms on one server process
- 0 places where user text becomes HTML

## What's next

The code is finished and tested. Deployment is optional and not confirmed: Baithak is complete as a project you can run with Docker Compose, and a hosted copy may or may not follow. If it does, it starts with several server processes behind the proxy, because one process falls behind past about 300 sockets.

The source is on [GitHub](https://github.com/ahmershahdev/baithak), and I wrote up the lesson underneath all of it in [The socket is not the source of truth](/blogs/the-socket-is-not-the-source-of-truth).


---

*Originally published at https://ahmershah.dev/blogs/baithak-case-study-real-time-team-chat — © Syed Ahmer Shah*
