Code Monkey home page Code Monkey logo

websocket-chat's Introduction

Chat-Pyton3-WS-Kafka-Redis-MySql-Docker

Image alt

  • Client uses a Websocket to connect to 'websocket service'
  • Client can register with a login and password using identity service
  • Any service can get user id by using identity service

Installation & Run

# Download this project
git clone https://github.com/MaximRobota/websocket-chat

Before running services, you should set the database config with yours or set the your database config with my values on .env

MYSQL_HOST=db
MYSQL_USER=user
MYSQL_PASSWORD=devpass
MYSQL_PORT=3398
MYSQL_DB=mysql_database
Build and Run
cd websocket-chat
docker-compose build
docker-compose up
API Endpoints : http://127.0.0.1:5051 (:5052, adminer :8080)

Structure

├── identity-service                // Our API core handlers
├── message-persistence-service     // Check and save events
├── websocket-service               // Two-way interactive communication session between the user's browser and a server
├── tests
├── docker-compose.yml

Identity service:

/auth/register (name, pass) -> token
/auth/login (name, pass) -> token
/auth/getIdByToken -> int (id)

Web socket service

  • Uses kafka to post messages that need saving (queue “message_save”)
  • Listens to kafka (queue “message_events”)
  • Checks that a user is a valid user (id service) and only then posts the message to “message_save”
  • Sends updates about messages to correct users
  • If a user with an invalid token sends a message ignore it and log a warning to stdout

Message persistence service

  • Listens on “message_save” queue
  • Saves messages to database and rollback if an error occurs:
  • In case of an error log error to stdout
  • Send a message error to “message_events”
  • In case of message is saved send message id and other data to “message_events”

Redis

  • Redis can be used as a key-value storage by websocket service:
    • Save user id and message UUID (provided by the client)
    • Is required to keep track on which messages are sent by which users

Client

  • Connect to a websocket
  • Sends message text, user_to_id and a random UUID (generated by client)
  • The message UUID is used to identify the message until the message is actually allotted a database id (until it is saved)
  1. Client sends:
{ 
	uuid: “a62f2bf7-cfe7-48d6x-ad53-956e41b0769b”,
	Message: “hi”,
	user_to : 2
}

With jwt_token in headers (at connection time)

  1. Websocket service:
    1. Marks the message UUID as the one sent by a user with some id (1? - get the id by token).

    2. When a message is saved Websocket service gets a message on “message_events” queue :

    {
    	uuid: “a62f2bf7-cfe7-48d6x-ad53-956e41b0769b”,
    	state: “Saved”
    }
    
  2. Websocket service looks up the corresponding websocket handle to send the event (saved)to the correct user and sends
    {
    	uuid: “a62f2bf7-cfe7-48d6x-ad53-956e41b0769b”,
    	state: “Saved”
    }
    
  3. Client gets the message and can identify the correct message to mark it as “successfully sent”.

General:

  • Every service is a docker container.
  • Every service is written in python 3 (3.7 +)
  • Use docker-compose to run the whole stack
  • Expect every service to run on the same host (localhost)
  • Every service runs on some port
  • This port is defined as an environment variable (ENV_VAR)
  • Docker-compose.yaml should define the ports for each service (do not hardcode the port values, use environment variables only)

Example:

import os 
os.environ[“KAFKA_PORT”] / os.environ[“ID_SERVICE_PORT”]/ ...


web_socket_service:
	Image:
Build: ..

Environment:
	KAFKA_PORT: 9092
persistence_service:
	Image:
Build: ..

Environment:
	KAFKA_PORT: 9092 

Acceptance criteria

  • Docker-compose -f docker_compose.yaml up
  • Python3 client_test.py
  • Registers 2 users,
  • User 1 sends a message to user 2
  • assert ( wait for 20 secs) that the message is saved
  • Message uuid matches the one we created
  • Message state is “saved”

Plan

  • Use docker-compose up to start for all services
  • JWT Authentication
  • Flask app for identity service
  • Create schema for database MySql
  • Implement Kafka Producer / Consumer
  • 2 processes
    1. sends a message
    2. prints the received message
  • Implement Redis (did not find application in the project)
  • Implement Kubernetes (basic test without using CP3-WS)

websocket-chat's People

Contributors

maximrobota avatar robertgolosynsky 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.