Skip to content

thecodephilic-guy/go-chat-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Chat Server

A real-time WebSocket-based chat server built with Go, following the "Let's Go Further" book's project structure.

Features

  • 🚀 Real-time messaging using WebSockets
  • 💬 Single global chat room
  • 📸 Media sharing support (images and files)
  • 🔄 No data persistence (ephemeral chat)
  • 🛡️ Rate limiting and CORS support
  • 📝 Structured JSON logging
  • 🎯 Clean architecture following Go best practices

Project Structure

go-chat-server/
├── bin/                    # Compiled binaries
├── cmd/
│   └── api/               # Application entry point and HTTP handlers
├── internal/
│   ├── chat/              # Chat hub and client logic
│   ├── jsonlog/           # Structured JSON logger
│   └── validator/         # Input validation helpers
├── go.mod                 # Module dependencies
└── Makefile              # Build automation

Quick Start

Prerequisites

  • Go 1.21 or higher

Installation

  1. Clone the repository:
cd /Users/sohail/Developer/go-chat-server
  1. Install dependencies:
go mod download
  1. Run the server:
make run/api
# or
go run ./cmd/api

The server will start on http://localhost:4000

API Endpoints

  • GET /v1/healthcheck - Health check endpoint
  • GET /v1/ws - WebSocket endpoint for chat

Message Format

Messages are sent as JSON over WebSocket:

{
  "type": "text|image|file|system",
  "username": "YourUsername",
  "content": "Your message content",
  "timestamp": "2026-02-11T12:00:00Z",
  "metadata": {
    "filename": "image.jpg",
    "mimetype": "image/jpeg",
    "size": 12345
  }
}

Message Types

  • text - Regular text message
  • image - Image (base64 encoded or URL)
  • file - File attachment (base64 encoded or URL)
  • system - System notification

Testing with Browser Console

Open your browser console and run:

const ws = new WebSocket('ws://localhost:4000/v1/ws');

ws.onopen = () => {
  console.log('Connected to chat server');
  
  // Send a text message
  ws.send(JSON.stringify({
    type: 'text',
    username: 'TestUser',
    content: 'Hello, World!'
  }));
};

ws.onmessage = (event) => {
  const message = JSON.parse(event.data);
  console.log('Received:', message);
};

ws.onerror = (error) => {
  console.error('WebSocket error:', error);
};

ws.onclose = () => {
  console.log('Disconnected from chat server');
};

Development

Run the server

make run/api

Build binaries

make build/api

Code quality checks

make audit

Configuration

The server accepts the following command-line flags:

  • -port - Server port (default: 4000)
  • -env - Environment (development|staging|production)
  • -cors-trusted-origins - Trusted CORS origins (can be specified multiple times)

Example:

go run ./cmd/api -port=8080 -env=production -cors-trusted-origins="https://example.com"

Architecture

The application follows the "Let's Go Further" book's architecture:

  • cmd/api - Application-specific code (HTTP server, routes, handlers)
  • internal - Reusable packages that cannot be imported by external projects
  • Graceful shutdown - Properly handles SIGINT and SIGTERM signals
  • Middleware chain - Panic recovery, rate limiting, and CORS
  • Structured logging - JSON-formatted logs for easy parsing

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors