Skip to content
SuralmkPublic

About

Golang and react implementation of chat

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

GoChat - Real-Time Messaging Architecture

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.

GoChat Interface

🚀 Technical Architecture

Backend: Go, GORM & WebSockets

The backend is engineered for high concurrency using Go's native primitives.

  • Concurreny Model: Uses a centralized Hub and Client architecture. The Hub manages active connections using Go channels (register, unregister, broadcast) and protects state with sync.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 via bcrypt.
  • 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/websocket with dedicated readPump and writePump goroutines per client to ensure non-blocking I/O.

Frontend: React, TypeScript & Zustand

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 like ChatList and ChatMessage, 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.

User Search

🛠️ Features Implementation Details

Real-Time Update Logic

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.

Secure Account Management

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.

Permanent Room Deletion

The "Delete Chat" feature implements a cascading deletion strategy:

  1. Validation: Ensures the requesting user is a legitimate participant of the room.
  2. Atomic Deletion: Removes all message logs and the room record from Postgres in a structured database transaction.

📦 Project Structure

├── 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.

About

Golang and react implementation of chat

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages