GithubHelp home page GithubHelp logo

ttu / chat-server Goto Github PK

View Code? Open in Web Editor NEW
5.0 4.0 0.0 395 KB

Small implementation of a server for a chat application for testing Docker

License: MIT License

Dockerfile 5.15% C# 70.82% HTML 3.31% CSS 1.78% JavaScript 17.85% TypeScript 1.09%
csharp asp-net-core redis rabbitmq docker docker-compose react

chat-server's Introduction

Chat Server

Note: This project is for my own testing and a reference for Docker scripts

Server for a chat application.

Diagram

diagram

  • ChatServer
    • .NET Core Web API
  • ChatBroker
    • .NET Core Web API
    • Handles new messages from RabbitMQ
  • Client
    • React application
    • Send and receive messages
  • RabbitMQ
    • Queue for new messages
  • Redis
    • Client register
    • Has information on which server user is logged on
  • TODO Postgres
    • Messages that are not delivered to offline users
  • TODO Authentication
  • TODO User Service
    • Node.js Express
    • Search users
    • Uses status
    • Share message history between clients

Getting Started

Run with Docker Compose

# Developemnt
$ docker-compose build
$ docker-compose up

# Production
$ docker-compose -f docker-compose-prod.yml build
$ docker-compose -f docker-compose-prod.yml up

Run development environment

Install required containers:

# Create network
$ docker network create --attachable chat

# Redis
$ docker run -d --name chat-redis --network=chat -p 6379:6379 redis

# RabbitMQ
$ docker run -d --name chat-rabbit --network=chat -p 5672:5672 -p 15672:15672 rabbitmq:3-management

# Quick start/stop
$ docker start chat-redis && docker start chat-rabbit
$ docker stop chat-redis && docker stop chat-rabbit

Start project from Visual Studio.

Run projects with Docker

Start debuggable projects in containers.

# Build image
$ docker build -t chatserver:dev .

# Start container
$ docker run --rm -it -e "Connections__Redis=chat-redis" -e "Connections__RabbitMQ=chat-rabbit" -e "DOTNET_RUNNING_IN_CONTAINER=true" -p 5000:5000 -p 10222:22 --name chat-server --network=chat -v C:\src\GitHub\chat-server\src\ChatServer:/app/ -w /app chatserver:dev

# Login to container
$ docker exec -it chat-server /bin/bash

# Start SSH server (TODO: should be always on)
$ service ssh start

# Debug -> Attach to process -> SSH (localhost:10222 root:Docker!) 
# Attach to dotnet exec process
# Select Managed Managed (.NET Core for Unix)

If you don't care about debugging from container, just start clean image with dotnet watch

$ docker run --rm -it -e "Connections__Redis=chat-redis" -e "Connections__RabbitMQ=chat-rabbit" -e "DOTNET_RUNNING_IN_CONTAINER=true" -p 5000:5000 --name chat-server --network=chat -v C:\src\GitHub\chat-server\src\ChatServer:/app/ -w /app microsoft/dotnet:2.1-sdk dotnet watch run
$ docker run --rm -it -e "Connections__Redis=chat-redis" -e "Connections__RabbitMQ=chat-rabbit" -e "DOTNET_RUNNING_IN_CONTAINER=true" -p 5000:5000 --name chat-server --network=chat -v C:\src\GitHub\chat-server\src\ChatBroker:/app/ -w /app microsoft/dotnet:2.1-sdk dotnet watch run
Debug chat-app React application with VS Code

sourceMapPathOverrides in launch.jsonmust override folder structure in Docker container.

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "attach",
      "name": "Attach to Chrome",
      "port": 9222,
      "webRoot": "${workspaceFolder}/src",
      "sourceMapPathOverrides": {
        "/app/src/*": "${webRoot}/*"
      }
    }
  ]
}

Production

# Build image
$ docker build -f Dockerfile-prod -t chatapp:prod .

# Start prod React app
$ docker run -it -p 800:80 --rm --name chat-app-prod chatapp:prod

TODO

  • Docker doesn't set ASPNETCORE_ENVIRONMENT correctly on development compose
  • Authorization
  • Save messages temporarily to DB
  • When user logs in send not sent messages to him immediately
  • Handle same user with multiple clients on multiple servers

Notes

Redis Commander

https://www.npmjs.com/package/redis-commander

$ npm install -g redis-commander
$ redis-commander

Open: http://localhost:8081/

ChatServer

...

ChatBroker / RabbitMQ

...

ClientRegister / Redis

...

Authentication

...

Flow

# Client logs in

ChatServer: Login
 Authenticate
 Send update to client register
 Check if there is waiting messages for the client in the sent_queue

# Client sends a new message

ChatServer: New message /api/send
 Validate
 Send update to client register
 Send message to RabbitMQ

ChatBroker: New message from RabbitMQ
 Get receiver server from client register
 Save message to sent_queue
 Send to correct server /api/receive
  Success
   Remove message from sent_queue
  Fail
   Update message status to sent_queue_

ChatServer: New message /api/receive
 Send to receiver with WebSocket
 Send status to broker (success/fail)

Links

Routing

Performance

RabbitMQ life cycle

Integration tests

Docker networking

Docker

VS Debug

chat-server's People

Contributors

ttu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.