Skip to content

Mostafa-SAID7/Chat-Api

Repository files navigation

πŸ’¬ Chat API

License: MIT .NET ASP.NET Core

A modern, real-time chat REST API built with ASP.NET Core 9, SignalR, MongoDB, and Redis caching.

Quick Start β€’ Documentation β€’ Architecture β€’ Contributing


✨ Features

  • πŸš€ Real-time Messaging - WebSocket support via SignalR
  • πŸ” Secure Authentication - JWT tokens with BCrypt hashing
  • πŸ’Ύ Flexible Database - MongoDB with in-memory fallback
  • ⚑ Performance - Redis caching (optional)
  • πŸ“ File Storage - Local blob storage for media
  • πŸ“š API Documentation - Swagger UI included
  • πŸ—οΈ Clean Architecture - Repository + UoW + MediatR CQRS pattern
  • πŸ”„ Graceful Degradation - Works without external services

πŸ› οΈ Tech Stack

Component Technology
Framework ASP.NET Core 9
Architecture Repository + Unit of Work + MediatR (CQRS)
Real-time SignalR WebSockets
Database MongoDB (with in-memory fallback)
Caching Redis (optional)
Authentication JWT Bearer + BCrypt
API Docs Swagger/OpenAPI
Storage Local file system

πŸš€ Quick Start

Replit (Easiest - One Click)

Run on Replit

  1. Click the Replit badge above
  2. Click "Run" button
  3. Access API at the provided URL

Local Setup

git clone https://github.com/Mostafa-SAID7/Chat-Api.git
cd Chat-Api/apiContact
dotnet restore
dotnet run

The API will start on http://localhost:5000

Access Swagger UI: http://localhost:5000/swagger


πŸ“š Documentation

Full documentation is available in the /docs directory:


πŸ”Œ API Endpoints

Chat Hub (WebSocket)

Base URL: ws://localhost:5000/hubs/chat

Core Endpoints

POST   /api/auth/register       Register new user
POST   /api/auth/login          Login and get JWT
GET    /api/contacts            Get all contacts
POST   /api/contacts            Create new contact
GET    /api/messages            Get conversation history
POST   /api/messages            Send message

See API Documentation for complete endpoint reference.


πŸ—οΈ Project Structure

apiContact/
β”œβ”€β”€ Controllers/          # API endpoints
β”œβ”€β”€ Features/            # Feature-specific logic (CQRS)
β”œβ”€β”€ Hubs/               # SignalR WebSocket hubs
β”œβ”€β”€ Services/           # Business logic services
β”œβ”€β”€ Models/             # Data models/entities
β”œβ”€β”€ Data/               # Database context & repositories
β”œβ”€β”€ Middleware/         # Custom middleware
β”œβ”€β”€ Mappings/           # AutoMapper profiles
β”œβ”€β”€ Utilities/          # Helper utilities
β”œβ”€β”€ Properties/         # Project properties
β”œβ”€β”€ appsettings.json    # Configuration
└── Program.cs          # Application startup

πŸ” Authentication

The API uses JWT Bearer tokens for authentication:

  1. Register or login to get a token
  2. Include token in request header: Authorization: Bearer <token>
  3. Token expires after 24 hours

Example:

curl -H "Authorization: Bearer eyJhbGc..." http://localhost:5000/api/contacts

πŸ’‘ Usage Example

Register & Login

# Register
curl -X POST http://localhost:5000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{"email":"user@example.com","password":"SecurePass123"}'

# Login
curl -X POST http://localhost:5000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"user@example.com","password":"SecurePass123"}'

Send Message via WebSocket

const connection = new signalR.HubConnectionBuilder()
  .withUrl("http://localhost:5000/hubs/chat")
  .withAutomaticReconnect()
  .build();

connection.start();

connection.invoke("SendMessage", {
  contactId: "123",
  text: "Hello!",
  timestamp: new Date()
});

πŸ§ͺ Testing

cd apiContact
dotnet test

πŸ“¦ Development

Build

dotnet build

Watch Mode

dotnet watch run

See DEVELOPMENT.md for more details.


🀝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see LICENSE.txt for details.


πŸ€” FAQ

Q: Do I need MongoDB to run this?
A: No, it includes an in-memory fallback. MongoDB is optional.

Q: Can I use this in production?
A: Yes, with proper MongoDB and Redis setup. See SETUP.md.

Q: How do I enable real-time chat?
A: Connect to the SignalR hub at /hubs/chat for real-time messaging.


πŸ“ž Support

For issues, questions, or suggestions:


Made with ❀️ by the Chat API team

⬆ Back to top

About

A real-time chat REST API built with ASP.NET Core (.NET 9), SignalR, MongoDB (with in-memory fallback), and Redis (optional)

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors