Skip to content
forked from akoidan/pychat

webchat written in django via WebsSockets/Webrtc

License

Notifications You must be signed in to change notification settings

area55git/pychat

 
 

Repository files navigation

python python Scrutinizer Build pass Scrutinizer Code Quality Code Health Codacy Badge

This is free web (browser) chat, that features:

  • Send instant text messages via websockets.
  • Send: images, smiles, anchors, embedded youtube, giphy, code highlight
  • Make calls and video conference using Peer to peer WebRTC.
  • Share screen during call or conference
  • Send files directly to another PC (p2p) using WebRTC + FileSystem Api (Average speed is 25MByte/s, limited by RTCDataChannel speed, but already is twice more than 100Mbit wan)
  • Edit images with integrated painter (brush/line/reactangle/oval/flood fill/erase/crop/cpilboard paste/resize/rotate/zoom/add text/ctrl+a)
  • Login in with facebook/google oauth.
  • Send offline messages with Firebase push notifications
  • Responsive interface (bs like)+ themes
  • Admin interface with django-admin

Live demo: pychat.org

Table of contents

Brief description

Chat is written in Python with django. For handling realtime messages WebSockets are used: browser support on client part and asynchronous framework Tornado on server part. Messages are being broadcast by means of redis pub/sub feature using tornado-redis backend. Redis is also used as django session backend and for storing current users online. For video call WebRTC technology was used with stun server to make a connection, which means you will always get the lowest ping and the best possible connection channel. Client part doesn't use any javascript frameworks (like jquery or datatables) in order to get best performance. Chat written as a singlePage application, so even if user navigates across different pages websocket connection doesn't break. Chat also supports OAuth2 login standard via FaceBook/Google. Css is compiled from sass. Server side can be run on any platform Windows, Linux, Mac with Python 2.7 and Python 3.x.Client (users) can use the chat from any browser with websocket support: IE11, Edge, Chrome, Firefox, Android, Opera, Safari...

How to run on my own server:

You can always use pychat.org, but if you want run chat yourself you have 3 options:

  • Test pychat with prebuilt docker image
  • Build docker image for production/test
  • Run chat for development
  • Set up for production w/o docker

Run test docker image

  • Download and run image: docker run -p 443:443 -p 8888:8888 deathangel908/pychat
  • Open https://localhost Please don't use this build for production, as it uses debug ssl certificate and lacks a few features. The docker files are here.

Build docker

  • Generate ssl certificates:
    • If you have bash installed: ./download_content.sh generate_certificate
    • You can also generate them manually and put into ./rootfs/etc/nginx/ssl/server.key and ./rootfs/etc/nginx/ssl/certificate.crt
  • Rename chat/settings_example.py to chat/settings.py. Open it and replace with your data according to comments. Everything in this file but SECRET_KEY is optional.
  • Build the image, you can use either single container or multiple:
    • Single container: docker build -t pychat . -f docker-all/Dockerfile. docker volume create pychat_data docker run -p 443:443 -p 8888:8888 -v pychat_data:/data pychat
    • Multiple container: docker-compose -f docker/docker-compose.yml up.
  • Open https://localhost

Development setup

The flow is the following

  • Install OS packages depending on your OS type
  • Bootstrap files
  • Configure Pycharm if you use it
  • Start services and check if it works

Install OS packages

This section depends on the OS you use. I tested full install on Windows/Ubuntu/CentOs/Archlinux/Archlinux(rpi2 armv7). pychat.org currently runs on Archlinux rpi2.

  1. Install python with pip. Any version Python2.7 or Python 3.x both are supported.
  2. Add pip and python to PATH variable.
  3. Install redis. Get the newest version or at least 2.8.
  4. Install sassc. Add sassc command path to PATH variable.
  5. Install mysql. You basically need mysql server and python connector.
  6. You also need to install python's mysqlclient. If you want to compile one yourself you need to vs2015 tools. You can download visual-studio and install Common Tools for Visual C++ 2015. You need to run setup as administrator. The only connector can be found here. The wheel (already compiled) connectors can be also found here Mysqlclient. Use pip to install them.
  7. Add bash commands to PATH variable. Cygwin or git's will do find.(for example if you use only git PATH=C:\Program Files\Git\usr\bin;C:\Program Files\Git\bin). Also add there new environment variable PYCHAT_CONFIG=local
  1. Install required packages: apt-get install python pip mysql-server ruby
  2. Install redis database: add-apt-repository -y ppa:rwky/redis; apt-get install -y redis-server
  3. Install sassc. You can find instructions here. Or maybe you can find packet in ubuntu repository. Alternatively you can use any other sass.
  4. echo 'export PYCHAT_CONFIG=local' >> /etc/profile
  1. With py3: pacman -S unzip python python-pip redis mariadb sassc. yaourt -S aur/python-mysqlclient. For py2 use extra/mysql-python
  2. If you just installed mariadb you need to initialize it: mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql.
  3. echo 'export PYCHAT_CONFIG=local' >> /etc/profile

Bootstrap files:

  1. I use 2 git repos in 2 project directory. So you probably need to rename excludeMAINfile to .gitignoreor create link to exclude. ln -rsf .excludeMAIN .git/info/exclude
  2. Rename chat/settings_example.py to chat/settings.py. Modify file according to the comments in it.
  3. Install VirtualEnv if you don't have it. pip install virtualenv.
  4. Create virtualEnv virtualenv --system-site-packages .venv and activate it .venv/bin/activate
  5. Install python packages with pip install -r requirements.txt.
  6. Create database: echo "create database pychat CHARACTER SET utf8 COLLATE utf8_general_ci" | mysql.If you need to add remote access to mysql: CREATE USER 'root'@'192.168.1.0/255.255.255.0'; GRANT ALL ON * TO root@'192.168.1.0/255.255.255.0';
  7. Fill database with tables: ./manage.py init_db && ./manage.py sync_db
  8. Populate project files: sh download_content.sh all

Configure Pycharm if you use it:

  1. Enable django support. Go to Settings -> Django -> Enable django support.
  • Django project root: root directory of your project. Where .git asides.
  • Put Settings: to chat/settings.py
  • 'Environment variables: PYCHAT_CONFIG=local
  1. Settings -> Project pychat -> Project Interpreter -> Cogs in right top -> 'Add' -> Virtual Environment -> Existing environment -> Interpereter = pychatdir/.venv/bin/python. Click ok. In previous menu on top 'Project interpreter` select the interpriter you just added.
  2. Settings -> Project: pychat -> Project structure
  • You might want to exclude: .idea, chat/static/css
  • mark templates directory as Template Folder
  1. Add start scripts:
  • Add server script: Run -> Edit configuration -> Django server -> Checkbox Custom run command runsslserver . Leave port 8000 as it it. Click on Environment Variable and set PYCHAT_CONFIG=local
  • Add tornado script: Run -> Edit configuration -> Django server -> Checkbox Custom run command start_tornado. Remove port value. Click on Environment Variable and set PYCHAT_CONFIG=local
  1. Pycharm sassc fewatcher. I use the latest sassc implementation for libsass, since it's the fastest ones, along with chrome workspace feature you can edit sass directly in browser or with jetbrains filewatcher. Latest sassc also allows inline sourcemap (base64 map directly in css file) that correctly shows source files in chrome. --sourcemap=inline does that. If your sassc doesn't have inline sourcemap support, please remove flag from argument command below. Also you you use any implementation you want, it's just a suggestion.
  • Go to Jetbrains Settings -> Tools -> FileWatchers -> Click add -> Sass:
  • arguments: --no-cache --update $FilePath$:$ProjectFileDir$/chat/static/css/$FileNameWithoutExtension$.css --style expanded. Or for newest sass $FilePath$ $ProjectFileDir$/chat/static/css/$FileNameWithoutExtension$.css --sourcemap=inline
  • working directory: $ProjectFileDir$/chat/static/sass
  • output files to refresh: $ProjectFileDir$/chat/static/css/

Start services and run:

  • Start mysql server if it's not started.
  • Start session holder: redis-server
  • Start webSocket listener: python manage.py start_tornado
  • Start the Chat: python manage.py runsslserver 0.0.0.0:8000
  • Open in browser https://127.0.0.1:8000.
  • If you get an ssl error on establishing websocket connection in browser (at least in Firefox, chrome should be fine), that's because you're using self-assigned certificate (provided by django-sslserver).You need to add security exception for websocket API_PORT (8888). Open https://localhost:8888 to do that.

Production setup

Further instructions assume that you're executing them from project directory. First of of you need to install packages for Archlinux/CentOS and then follow the Common flow

Archlinux prod

Services commands for Archlinux:

  • Start services: packages=( mysqld redis uwsgi tornado nginx postfix ) ; for package in "${packages[@]}" ; do systemctl enable $package; done;
  • Enabling autostart: packages=( redis nginx postfix mysqld uwsgi tornado) ; for package in "${packages[@]}" ; do systemctl start $package; done;

Installing packages for Archlinux:

  • Install packages pacman -S nginx postfix gcc jansson.
  • Install pip install uwsgi. You also can install uwsgi and uwsgi-plugin-python via pacman but I found pip's package more stable.
  • Follow the instructions in Archlinux.

CentOS prod

Installing packages for Archlinux

  • Add alias yum="python2 $(which yum)" to /etc/bashrc if you use python3
  • Install packages yum install nginx, python34u, uWSGI, python34u-pip, redis, mysql-server, mysql-devel, postfix, mailx, ruby, rubygems
  • Install sass gem install sass

Services commands for Archlinux:

  • Start services: packages=( redis-server nginx postfix mysqld uwsgi tornado) ; for package in "${packages[@]}" ; do service $package start; done;
  • Enabling autostart: chkconfig mysqld on; chkconfig uwsgi on; chkconfig tornado on; chkconfig redis on; chkconfig postfix on

Common

  • Set environment variable PYCHAT_CONFIG=prod
  • Follow the instructions in Boostrap files.
  • For production I would recommend to clone repository to /srv/http/pychat. If you cloned project into different directory than /srv/http/pychat replace all absolute paths in config files. You can use download_content.sh rename_root_directory to do that.
  • Replace all occurrences of pychat.org in rootfs for your domain. You can use ./download_content.sh rename_domain your.new.domain.com
  • Also check rootfs/etc/nginx/nginx.conf you may want to merge location /photo and location /static into main server conf. You need all of this because I used subdomain for static urls/
  • HTTPS is required for webrtc calls so you need to enable ssl:
    • Obtain ceritifcate.
      • Register online. There're a lot of free and paid services. Like comodo or startssl(only 1 year free). Here's instructions for startssl.
        • Follow the instructions in https://www.startssl.com.
        • Start postfix service (it's required to verify that you have access to domain)
        • Send validation email to domain webmaster@pychat.org
        • Apply verification code from /root/Maildir/new/<<time>> (you may also need to disable ssl in /etc/postfix/main.cf since it's enabled by default).
        • You can generate server.key and get certificate from https://www.startssl.com/Certificates/ApplySSLCert .
      • Generate custom certificate.
        • execute ./download_content.sh generate_certificate
        • Or generate by yourself
    • Now you got certificate and you want to put files according to nginx.conf. Copy server key into ./rootfs/etc/nginx/ssl/server.key and certificate into ./rootfs/etc/nginx/ssl/certificate.crt.
    • Don't forget to change owner of files to http user chown -R http:http /etc/nginx/ssl/
  • Copy config files to rootfs cp rootfs / -r . Change owner of project to http user: chown -R http:http. And reload systemd config systemctl daemon-reload.
  • Generate postfix postman: postmap /etc/postfix/virtual; postman /etc/aliases
  • Add django admin static files: python manage.py collectstatic
  • Execute start services and if you need enablign autostart commands described for Archlinux or CentOS
  • Open in browser https://your.domain.com. Note that by default nginx accepts request by domain.name rather than ip.
  • If something doesn't work you want to check pychat/logs directory. If there's no logs in directory you may want to check service stdout: sudo journalctl -u YOUR_SERVICE. Check that user http has access to you project directory.

Contributing:

Take a look at Contributing.md for more info details.

TODO list

About

webchat written in django via WebsSockets/Webrtc

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 39.3%
  • CSS 31.9%
  • Python 21.8%
  • HTML 3.1%
  • Shell 2.2%
  • CMake 1.6%
  • C++ 0.1%