Giter VIP home page Giter VIP logo

django-channels-chat's Introduction

ChatApp

A small functional person-to-person message center application built using Django. It has a REST API and uses WebSockets to notify clients of new messages and avoid polling.

Architecture

  • When a user logs in, the frontend downloads the user list and opens a Websocket connection to the server (notifications channel).
  • When a user selects another user to chat, the frontend downloads the latest 15 messages (see settings) they've exchanged.
  • When a user sends a message, the frontend sends a POST to the REST API, then Django saves the message and notifies the users involved using the Websocket connection (sends the new message ID).
  • When the frontend receives a new message notification (with the message ID), it performs a GET query to the API to download the received message.

Scaling

Requests

"Because Channels takes Django into a multi-process model, you no longer run everything in one process along with a WSGI server (of course, you’re still free to do that if you don’t want to use Channels). Instead, you run one or more interface servers, and one or more worker servers, connected by that channel layer you configured earlier."

In this case, I'm using the In-Memory channel system, but could be changed to the Redis backend to improve performance and spawn multiple workers in a distributed environment.

Please take a look at the link below for more information: https://channels.readthedocs.io/en/latest/introduction.html

Database

For this demo, I'm using a simple MySQL setup. If more performance is required, a MySQL cluster / shard could be deployed.

PD: I'm using indexes to improve performance.

Assumptions

Because of time constraints this project lacks of:

  • User Sign-In / Forgot Password
  • User Selector Pagination
  • Good Test Coverage
  • Better Comments / Documentation Strings
  • Frontend Tests
  • Modern Frontend Framework (like React)
  • Frontend Package (automatic lintin, building and minification)
  • Proper UX / UI design (looks plain bootstrap)

Run

  1. Create and activate a virtualenv (Python 3)
virtualenv -p python3 venv
source venv/bin/activate
  1. Install requirements
pip install -r requirements.txt
  1. Create a MySQL database
CREATE DATABASE chat CHARACTER SET utf8;
  1. Init database
./manage.py migrate
  1. Run tests
./manage.py test
  1. Create admin user
./manage.py createsuperuser
  1. Run development server
./manage.py runserver

To override default settings, create a local_settings.py file in the chat folder.

Message prefetch config (load last n messages):

MESSAGES_TO_LOAD = 15

django-channels-chat's People

Contributors

narrowfail avatar spkuehl avatar

Watchers

 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.