GoChat is a high-performance, real-time messaging application built with a focus on concurrency, scalability, and seamless user experience. It leverages a robust Go backend and a reactive React frontend to provide instant communication.
The backend is engineered for high concurrency using Go's native primitives.
- Concurreny Model: Uses a centralized
HubandClientarchitecture. TheHubmanages active connections using Go channels (register,unregister,broadcast) and protects state withsync.Mutex. - Data Persistence: Powered by GORM with a PostgreSQL driver. It features automated schema migrations and custom hooks (like
BeforeCreate) for UUID generation and password hashing viabcrypt. - OAuth2 Integration: Implements Google OAuth2 natively using
golang.org/x/oauth2, allowing for secure external authentication and automatic profile provisioning. - JWT Authentication: A custom middleware layer handles token validation using
golang-jwt/jwt/v5, ensuring secure access to specialized subrouters. - WebSocket Protocol: Utilizes
gorilla/websocketwith dedicatedreadPumpandwritePumpgoroutines per client to ensure non-blocking I/O.
The frontend is a modern SPA designed for low latency and state consistency.
- Real-Time Synchronization: Centralized through a custom
WebSocketContext. This provider maintains a single stable connection shared across components likeChatListandChatMessage, preventing redundant socket overhead. - State Management: Uses Zustand with persistent storage for session and UI state management.
- Dynamic Routing: Built with
react-router-dom, supporting nested layouts and dynamic parameterization for rooms and user profiles. - UI System: Developed using Tailwind CSS and shadcn/ui components for a premium, responsive interface featuring dark mode support and micro-animations.
Whenever a message is processed via POST /api/messages, the backend identifies the participants and broadcasts a new_message event through the Hub. The frontend ChatList listens to this event to fetch updated room statuses or update local state optimistically, while ChatMessage filters the payload to append only relevant messages to the active viewport.
Users can sign up via traditional email/password or use Google Login. For Google users, the Password field remains null, and the system enforces OAuth-only access for that specific account ID, ensuring robust security.
The "Delete Chat" feature implements a cascading deletion strategy:
- Validation: Ensures the requesting user is a legitimate participant of the room.
- Atomic Deletion: Removes all message logs and the room record from Postgres in a structured database transaction.
├── cmd/ # Application entry point
├── internal/
│ ├── auth/ # OAuth2 and JWT logic
│ ├── chat/ # Business logic for rooms and messages
│ ├── models/ # GORM models and DB initialization
│ ├── router/ # Mux routing and middleware
│ └── ws/ # WebSocket Hub and Client implementation
├── frontend/
│ ├── src/
│ │ ├── context/ # Shared WebSocket Context
│ │ ├── store/ # Zustand stores
│ │ └── components/# UI components (shadcn/ui)Built with ❤️ using Go and React.

