Architecture Overview - nself-org/nchat GitHub Wiki
Architecture Overview
nself-chat System Architecture - High-level design and technical overview.
Version: 1.0.0 | Last Updated: April 17, 2026
π― Executive Summary
nself-chat is a production-ready, white-label team communication platform built as a monorepo with separate backend and frontend concerns. It supports 6 platforms (Web, iOS, Android, Windows, macOS, Linux) from a single React codebase.
Key Design Principles:
- Separation of Concerns: Backend (nself CLI) and Frontend (React) are independent
- Code Reuse: Shared packages across all platform apps
- Type Safety: 100% TypeScript with zero errors
- Production Ready: Zero compromises on quality, security, or performance
ποΈ System Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β nself-chat v1.0.0 β
β Multi-Platform Chat System β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β²
β
βββββββββββββββββββββββ΄ββββββββββββββββββββββ
β β
βββββββββΌβββββββββ ββββββββββΌβββββββββ
β Frontend β β Backend β
β (React 19) βββββββββββββββββββββββββββ€ (nself CLI) β
β β GraphQL/WebSocket β β
ββββββββββββββββββ βββββββββββββββββββ
β β
β β
βββββββββΌβββββββββββββββββββββββββββββ ββββββββββΌββββββββββββββββββ
β Platform Apps β β Backend Services β
ββββββββββββββββββββββββββββββββββββββ€ ββββββββββββββββββββββββββββ€
β β’ Web (Next.js 15) β β β’ PostgreSQL (database) β
β β’ iOS (Capacitor 6) β β β’ Hasura (GraphQL) β
β β’ Android (Capacitor 6) β β β’ Auth (Nhost) β
β β’ Windows (Electron 33) β β β’ MinIO (storage) β
β β’ macOS (Electron 33) β β β’ Redis (cache) β
β β’ Linux (Electron 33) β β β’ MeiliSearch (search) β
ββββββββββββββββββββββββββββββββββββββ β β’ Mailpit (email dev) β
β β’ Nginx (proxy) β
ββββββββββββββββββββββββββββ
π Monorepo Structure
Project Layout
nself-chat/
βββ backend/ # Backend infrastructure (nself CLI)
β βββ migrations/ # SQL migrations (222 tables)
β βββ hasura/ # GraphQL metadata and permissions
β βββ nginx/ # Reverse proxy configuration
β βββ postgres/ # PostgreSQL init scripts
β βββ auth/ # Auth service configuration
β βββ schema.dbml # Database schema (DBML format)
β βββ docker-compose.yml # Generated by nself build
β βββ .env # Local environment config
β βββ README.md # Backend documentation
β
βββ frontend/ # Frontend monorepo (all platforms)
β βββ apps/
β β βββ web/ # Next.js 15 web application
β β β βββ src/ # Source code
β β β β βββ app/ # Next.js App Router
β β β β βββ components/
β β β β βββ hooks/
β β β β βββ lib/
β β β β βββ services/
β β β βββ public/ # Static assets
β β β βββ package.json
β β β βββ README.md
β β β
β β βββ mobile/ # Capacitor mobile app
β β β βββ src/ # React source code
β β β βββ android/ # Native Android project
β β β βββ ios/ # Native iOS project
β β β βββ capacitor.config.json
β β β βββ README.md
β β β
β β βββ desktop/ # Electron desktop app
β β βββ src/
β β β βββ main/ # Main process (Node.js)
β β β βββ preload/# Preload scripts
β β β βββ renderer/# Renderer process (React)
β β β βββ adapters/# Platform adapters
β β βββ build/ # Build resources (icons)
β β βββ electron-builder.json
β β βββ README.md
β β
β βββ packages/ # Shared packages (code reuse)
β β βββ ui/ # Radix UI components
β β βββ config/ # Configuration types
β β βββ lib/ # Utility functions
β β βββ graphql/ # GraphQL client & queries
β β βββ auth/ # Authentication logic
β β βββ state/ # State management (Zustand)
β β βββ types/ # TypeScript types
β β
β βββ tooling/ # Development tooling
β β βββ eslint/ # Shared ESLint config
β β
β βββ package.json # Root workspace config
β βββ pnpm-workspace.yaml
β βββ README.md
β
βββ .wiki/ # Complete documentation (228 files)
β βββ Home.md # Documentation home
β βββ getting-started/
β βββ features/
β βββ guides/
β βββ api/
β βββ deployment/
β βββ ...
β
βββ .github/ # GitHub Actions CI/CD
β βββ workflows/ # 19 workflow files
β
βββ .claude/ # Claude Code context (gitignored)
β βββ v092/ # Current version tracking
β
βββ scripts/ # Build and utility scripts
βββ package.json # Root package.json
βββ README.md # Project overview
βββ LICENSE # MIT License
π§ Backend Architecture
nself CLI Stack
The backend uses nself CLI v1.0.6 for a complete BaaS (Backend as a Service) stack.
Core Services (required):
- PostgreSQL 16 - Primary database with 60+ extensions
- Hasura 2.x - GraphQL Engine with permissions
- Auth - Nhost Authentication service
- Nginx - Reverse proxy + SSL termination
Optional Services (configurable via .env):
- nself-admin - Admin UI (port 3021)
- MinIO - S3-compatible object storage
- Redis - Cache and session storage
- MeiliSearch - Full-text search engine
- Mailpit - Email testing (dev only)
- Monitoring Stack - Grafana, Prometheus, Loki, etc.
Database Schema
222 tables organized by feature:
- Core Tables: users, profiles, channels, messages, threads
- RBAC: roles, permissions, user_roles
- Features: reactions, bookmarks, pins, attachments
- Advanced: guilds, categories, broadcasts, bots
- Multi-Tenancy: workspaces, subscriptions, billing
- Analytics: events, metrics, audit_logs
Schema Format: DBML (Database Markup Language) in backend/schema.dbml
GraphQL API
Hasura provides:
- Auto-generated GraphQL API from PostgreSQL schema
- Real-time subscriptions via WebSocket
- Role-based permissions (RBAC)
- Remote schemas for custom business logic
- Actions for REST API integration
API Endpoints:
- GraphQL:
/v1/graphql - WebSocket:
/v1/graphql(ws://) - Admin Console:
/console
π» Frontend Architecture
Multi-Platform Strategy
One codebase β Six platforms using React 19:
| Platform | Technology | Build Output |
|---|---|---|
| Web | Next.js 15 (SSR/SSG) | Static HTML + JS |
| iOS | Capacitor 6 + WebView | .ipa (App Store) |
| Android | Capacitor 6 + WebView | .apk/.aab (Play Store) |
| Windows | Electron 33 | .exe installer |
| macOS | Electron 33 | .dmg disk image |
| Linux | Electron 33 | .AppImage/.deb/.rpm |
Shared Packages
Monorepo design maximizes code reuse:
// Example: Importing from shared packages
import { Button } from '@nself-chat/ui'
import { useAuth } from '@nself-chat/auth'
import { apolloClient } from '@nself-chat/graphql'
import { logger } from '@nself-chat/lib'
Benefits:
- β Single source of truth for types
- β Consistent UI across all platforms
- β Shared business logic
- β Easier testing and maintenance
Platform Adapters
Each platform has adapters for native features:
Mobile Adapters (frontend/apps/mobile/src/adapters/):
storage/- AsyncStorage β LocalStorage interfaceauth/- Biometric authentication (Face ID, Touch ID)notifications/- Push notifications (FCM/APNS)camera/- Photo capture and gallery accessnetwork/- Connectivity monitoring
Desktop Adapters (frontend/apps/desktop/src/adapters/):
storage/- electron-store β LocalStorage interfacenotifications/- Native desktop notificationsclipboard/- System clipboard accessfilesystem/- File system operationswindow/- Window management
State Management
Zustand 5.0 for local state:
// Example: Auth store
import { create } from 'zustand'
const useAuthStore = create((set) => ({
user: null,
login: (user) => set({ user }),
logout: () => set({ user: null }),
}))
Apollo Client 3.12 for server state:
// Example: GraphQL query
const { data, loading } = useQuery(GET_MESSAGES, {
variables: { channelId },
})
UI Component Library
Radix UI primitives + Tailwind CSS styling:
- Accessible (WCAG 2.1 AA compliant)
- Unstyled primitives (full control)
- Dark/light mode support
- Consistent across all platforms
π Security Architecture
Authentication Flow
ββββββββββββ ββββββββββββ ββββββββββββ
β Client βββββββΆβ Auth βββββββΆβ Hasura β
β (Browser)β β Service β β GraphQL β
ββββββββββββ ββββββββββββ ββββββββββββ
β β β
β β β
βΌ βΌ βΌ
JWT Token JWT Secret JWT Validation
(httpOnly (HMAC-SHA256) (x-hasura-* claims)
cookie)
Auth Methods:
- Email + Password
- Magic Links (passwordless)
- 11 OAuth Providers (Google, GitHub, Apple, etc.)
- 2FA/TOTP (optional)
- Biometric (mobile)
Authorization (RBAC)
5 built-in roles with granular permissions:
- Owner - Full access to everything
- Admin - User and channel management
- Moderator - Content moderation
- Member - Standard user
- Guest - Limited read-only access
Permission Examples:
// Hasura permission rules (simplified)
{
"select": {
"filter": {
"_or": [
{ "channel.type": "public" },
{ "channel.members.user_id": "X-Hasura-User-Id" }
]
}
}
}
Data Security
- End-to-End Encryption: Signal Protocol (Double Ratchet)
- Transport Security: HTTPS/TLS 1.3
- Database Encryption: At-rest encryption (optional)
- Secrets Management: Environment variables + vault
- Input Sanitization: DOMPurify for XSS prevention
- CSRF Protection: SameSite cookies
- Rate Limiting: Redis-based rate limiting
π Deployment Architecture
Development Environment
Developer Machine
βββ Backend (Docker)
β βββ 8 services running via nself CLI
βββ Frontend (Node.js)
βββ pnpm dev (port 3000)
Production Architecture (Recommended)
βββββββββββββββββββββββββββββββββββββββββββββββ
β Load Balancer β
β (nginx, Cloudflare, etc.) β
βββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β
βββββββββββ΄ββββββββββ
β β
βββββΌβββββ ββββββΌβββββ
β Web β β Backend β
β Nodes β β Cluster β
β (Next) β β(Hasura) β
ββββββββββ βββββββββββ
β β
β ββββββΌβββββ
β β DB β
ββββββββββββββββΆ(Postgres)
βββββββββββ
Deployment Options
| Option | Best For | Complexity |
|---|---|---|
| Vercel | Web app only | Low |
| Docker | Self-hosted | Medium |
| Kubernetes | Enterprise scale | High |
| AWS/GCP/Azure | Full control | High |
π Data Flow
Real-Time Messaging
User A Server User B
β β β
ββββββ(1) Send MsgββββββββΆβ β
β (GraphQL) β β
β β β
ββββββ(2) Confirm βββββββββ β
β (200 OK) β β
β β β
β βββ(3) Push MsgβββββββββββΆβ
β β (WebSocket) β
β β β
β ββββββ(4) Ackββββββββββββββ
β β β
Steps:
- User A sends message via GraphQL mutation
- Server validates, saves to DB, returns confirmation
- Server pushes message to User B via WebSocket subscription
- User B acknowledges receipt
File Upload Flow
Client Frontend MinIO Database
β β β β
ββ(1) Select FileβββββββΆβ β β
β β β β
β ββ(2) Get Upload URLββΆβ β
β β β β
β βββ(3) Signed URLβββββ β
β β β β
βββ(4) Upload DirectβββββΌβββββββββββββββββββββΆβ β
β β β β
β ββ(5) Save MetadataβββΌβββββββββββββββββΆβ
β β β β
βββ(6) Successβββββββββββ β β
Benefits:
- Direct upload to S3 (faster)
- No frontend server load
- Scalable file storage
π Development Workflow
Git Branch Strategy
main (production)
β
βββ develop (staging)
β
βββ feature/new-feature
βββ fix/bug-fix
βββ refactor/code-cleanup
CI/CD Pipeline
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β Commit ββββββΆβ CI Checks ββββββΆβ Deploy β
β (GitHub) β β (Actions) β β (Vercel) β
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β
ββββββββ΄βββββββ
β β
βββββββΌβββββ βββββββΌβββββ
β Lint β β Test β
β Type β β Build β
ββββββββββββ ββββββββββββ
CI Checks:
- β Linting (ESLint)
- β Type checking (TypeScript)
- β Unit tests (Jest)
- β Integration tests
- β E2E tests (Playwright)
- β Security scans (TruffleHog)
- β Build verification
π Performance Optimizations
Frontend
- Code Splitting: Dynamic imports, route-based splitting
- Image Optimization: Next.js Image component, Sharp.js
- Bundle Analysis: Webpack Bundle Analyzer
- Lazy Loading: React.lazy for heavy components
- Memoization: React.memo, useMemo, useCallback
- Virtual Scrolling: For long message lists
Backend
- Database Indexes: Optimized indexes for common queries
- Connection Pooling: PgBouncer for PostgreSQL
- Caching: Redis for frequently accessed data
- Query Optimization: Hasura performance hints
- CDN: Static assets on CDN (Cloudflare, etc.)
Monitoring
- Sentry: Error tracking and performance monitoring
- Grafana: Metrics dashboards
- Prometheus: Metrics collection
- Loki: Log aggregation
π§ͺ Testing Strategy
Test Pyramid
βββββββββββββββ
β E2E Tests β (Playwright)
β ~150 β
βββββββββββββββ
βββββββββββββββββ
βIntegration β (Jest)
β Tests β
β ~250 β
βββββββββββββββββ
ββββββββββββββββββββββ
β Unit Tests β (Jest)
β ~600 β
ββββββββββββββββββββββ
Coverage Goals:
- Unit Tests: >80%
- Integration Tests: Critical paths
- E2E Tests: User journeys
π Technology Stack Summary
Frontend
| Category | Technology | Version |
|---|---|---|
| Framework | Next.js | 15.1.6 |
| UI Library | React | 19.0.0 |
| Language | TypeScript | 5.7.3 |
| Styling | Tailwind CSS | 3.4.17 |
| State | Zustand | 5.0.3 |
| GraphQL | Apollo Client | 3.12.8 |
| UI Components | Radix UI | Latest |
| Forms | React Hook Form | 7.54.2 |
| Validation | Zod | 3.24.1 |
Backend
| Category | Technology | Version |
|---|---|---|
| Database | PostgreSQL | 16 |
| GraphQL | Hasura | 2.x |
| Auth | Nhost Auth | Latest |
| Storage | MinIO | Latest |
| Search | MeiliSearch | 1.x |
| Cache | Redis | 7.x |
| Proxy | Nginx | Latest |
| Infrastructure | nself CLI | 0.4.2 |
Mobile/Desktop
| Category | Technology | Version |
|---|---|---|
| Mobile | Capacitor | 6.2 |
| Desktop | Electron | 33.2 |
| Build Tool | Vite | 6.0 |
DevOps
| Category | Technology | Purpose |
|---|---|---|
| CI/CD | GitHub Actions | Automation |
| Monitoring | Sentry | Error tracking |
| Logging | Loki | Log aggregation |
| Metrics | Prometheus | Metrics collection |
| Dashboards | Grafana | Visualization |
π Additional Resources
- Backend README - Backend setup and services
- Frontend README - Frontend monorepo details
- Database Schema - Complete schema documentation
- API Reference - GraphQL API documentation
- Deployment Guide - Production deployment
Next Steps: Quick Start | Installation | Deployment