├── README.md
├── chat
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-38.pyc
│ ├── __init__.cpython-39.pyc
│ ├── admin.cpython-38.pyc
│ ├── admin.cpython-39.pyc
│ ├── apps.cpython-38.pyc
│ ├── apps.cpython-39.pyc
│ ├── consumers.cpython-38.pyc
│ ├── consumers.cpython-39.pyc
│ ├── models.cpython-38.pyc
│ ├── models.cpython-39.pyc
│ ├── routing.cpython-38.pyc
│ ├── routing.cpython-39.pyc
│ ├── urls.cpython-38.pyc
│ ├── urls.cpython-39.pyc
│ ├── views.cpython-38.pyc
│ └── views.cpython-39.pyc
├── admin.py
├── apps.py
├── consumers.py
├── migrations
│ ├── __init__.py
│ └── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ └── __init__.cpython-39.pyc
├── models.py
├── routing.py
├── templates
│ └── chat
│ │ └── lobby.html
├── tests.py
├── urls.py
└── views.py
├── db.sqlite3
├── manage.py
├── mywebsite
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-38.pyc
│ ├── __init__.cpython-39.pyc
│ ├── asgi.cpython-38.pyc
│ ├── asgi.cpython-39.pyc
│ ├── settings.cpython-38.pyc
│ ├── settings.cpython-39.pyc
│ ├── urls.cpython-38.pyc
│ ├── urls.cpython-39.pyc
│ └── wsgi.cpython-39.pyc
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
└── requirements.txt
/README.md:
--------------------------------------------------------------------------------
1 | # django-channels-basic-chat
2 |
3 | Tutorial link: https://youtu.be/cw8-KFVXpTE
4 |
5 |
6 | ## Cloning and starting project
7 | 1. - git clone https://github.com/divanov11/django-channels-basic-chat
8 | 2. - cd django-channels-basic-chat
9 | 3. - pip install -r requirements.txt
10 | 4. - python manage.py runserver
11 |
--------------------------------------------------------------------------------
/chat/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__init__.py
--------------------------------------------------------------------------------
/chat/__pycache__/__init__.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/__init__.cpython-38.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/__init__.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/__init__.cpython-39.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/admin.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/admin.cpython-38.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/admin.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/admin.cpython-39.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/apps.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/apps.cpython-38.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/apps.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/apps.cpython-39.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/consumers.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/consumers.cpython-38.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/consumers.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/consumers.cpython-39.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/models.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/models.cpython-38.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/models.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/models.cpython-39.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/routing.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/routing.cpython-38.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/routing.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/routing.cpython-39.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/urls.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/urls.cpython-38.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/urls.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/urls.cpython-39.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/views.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/views.cpython-38.pyc
--------------------------------------------------------------------------------
/chat/__pycache__/views.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/__pycache__/views.cpython-39.pyc
--------------------------------------------------------------------------------
/chat/admin.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 |
3 | # Register your models here.
4 |
--------------------------------------------------------------------------------
/chat/apps.py:
--------------------------------------------------------------------------------
1 | from django.apps import AppConfig
2 |
3 |
4 | class ChatConfig(AppConfig):
5 | default_auto_field = 'django.db.models.BigAutoField'
6 | name = 'chat'
7 |
--------------------------------------------------------------------------------
/chat/consumers.py:
--------------------------------------------------------------------------------
1 | import json
2 | from channels.generic.websocket import WebsocketConsumer
3 | from asgiref.sync import async_to_sync
4 |
5 | class ChatConsumer(WebsocketConsumer):
6 | def connect(self):
7 | self.room_group_name = 'test'
8 |
9 | async_to_sync(self.channel_layer.group_add)(
10 | self.room_group_name,
11 | self.channel_name
12 | )
13 |
14 | self.accept()
15 |
16 |
17 | def receive(self, text_data):
18 | text_data_json = json.loads(text_data)
19 | message = text_data_json['message']
20 |
21 | async_to_sync(self.channel_layer.group_send)(
22 | self.room_group_name,
23 | {
24 | 'type':'chat_message',
25 | 'message':message
26 | }
27 | )
28 |
29 | def chat_message(self, event):
30 | message = event['message']
31 |
32 | self.send(text_data=json.dumps({
33 | 'type':'chat',
34 | 'message':message
35 | }))
--------------------------------------------------------------------------------
/chat/migrations/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/migrations/__init__.py
--------------------------------------------------------------------------------
/chat/migrations/__pycache__/__init__.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/migrations/__pycache__/__init__.cpython-38.pyc
--------------------------------------------------------------------------------
/chat/migrations/__pycache__/__init__.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/chat/migrations/__pycache__/__init__.cpython-39.pyc
--------------------------------------------------------------------------------
/chat/models.py:
--------------------------------------------------------------------------------
1 | from django.db import models
2 |
3 | # Create your models here.
4 |
--------------------------------------------------------------------------------
/chat/routing.py:
--------------------------------------------------------------------------------
1 | from django.urls import re_path
2 | from . import consumers
3 |
4 | websocket_urlpatterns = [
5 | re_path(r'ws/socket-server/', consumers.ChatConsumer.as_asgi())
6 | ]
--------------------------------------------------------------------------------
/chat/templates/chat/lobby.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Lobby
7 |
8 |
9 |
10 |
11 | Lets chat!
12 |
13 |
16 |
17 |
18 |
19 |
48 |
49 |
--------------------------------------------------------------------------------
/chat/tests.py:
--------------------------------------------------------------------------------
1 | from django.test import TestCase
2 |
3 | # Create your tests here.
4 |
--------------------------------------------------------------------------------
/chat/urls.py:
--------------------------------------------------------------------------------
1 | from django.urls import path
2 | from . import views
3 |
4 | urlpatterns = [
5 | path('', views.lobby)
6 | ]
--------------------------------------------------------------------------------
/chat/views.py:
--------------------------------------------------------------------------------
1 | from django.shortcuts import render
2 |
3 | # Create your views here.
4 |
5 | def lobby(request):
6 | return render(request, 'chat/lobby.html')
--------------------------------------------------------------------------------
/db.sqlite3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/db.sqlite3
--------------------------------------------------------------------------------
/manage.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | """Django's command-line utility for administrative tasks."""
3 | import os
4 | import sys
5 |
6 |
7 | def main():
8 | """Run administrative tasks."""
9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mywebsite.settings')
10 | try:
11 | from django.core.management import execute_from_command_line
12 | except ImportError as exc:
13 | raise ImportError(
14 | "Couldn't import Django. Are you sure it's installed and "
15 | "available on your PYTHONPATH environment variable? Did you "
16 | "forget to activate a virtual environment?"
17 | ) from exc
18 | execute_from_command_line(sys.argv)
19 |
20 |
21 | if __name__ == '__main__':
22 | main()
23 |
--------------------------------------------------------------------------------
/mywebsite/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/mywebsite/__init__.py
--------------------------------------------------------------------------------
/mywebsite/__pycache__/__init__.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/mywebsite/__pycache__/__init__.cpython-38.pyc
--------------------------------------------------------------------------------
/mywebsite/__pycache__/__init__.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/mywebsite/__pycache__/__init__.cpython-39.pyc
--------------------------------------------------------------------------------
/mywebsite/__pycache__/asgi.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/mywebsite/__pycache__/asgi.cpython-38.pyc
--------------------------------------------------------------------------------
/mywebsite/__pycache__/asgi.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/mywebsite/__pycache__/asgi.cpython-39.pyc
--------------------------------------------------------------------------------
/mywebsite/__pycache__/settings.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/mywebsite/__pycache__/settings.cpython-38.pyc
--------------------------------------------------------------------------------
/mywebsite/__pycache__/settings.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/mywebsite/__pycache__/settings.cpython-39.pyc
--------------------------------------------------------------------------------
/mywebsite/__pycache__/urls.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/mywebsite/__pycache__/urls.cpython-38.pyc
--------------------------------------------------------------------------------
/mywebsite/__pycache__/urls.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/mywebsite/__pycache__/urls.cpython-39.pyc
--------------------------------------------------------------------------------
/mywebsite/__pycache__/wsgi.cpython-39.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/divanov11/django-channels-basic-chat/20ed02548083674d5a282e1851b5fe704d113d6a/mywebsite/__pycache__/wsgi.cpython-39.pyc
--------------------------------------------------------------------------------
/mywebsite/asgi.py:
--------------------------------------------------------------------------------
1 | """
2 | ASGI config for mywebsite project.
3 |
4 | It exposes the ASGI callable as a module-level variable named ``application``.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
8 | """
9 |
10 | import os
11 |
12 | from django.core.asgi import get_asgi_application
13 | from channels.routing import ProtocolTypeRouter, URLRouter
14 | from channels.auth import AuthMiddlewareStack
15 | import chat.routing
16 |
17 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mywebsite.settings')
18 |
19 | application = ProtocolTypeRouter({
20 | 'http':get_asgi_application(),
21 | 'websocket':AuthMiddlewareStack(
22 | URLRouter(
23 | chat.routing.websocket_urlpatterns
24 | )
25 | )
26 | })
27 |
--------------------------------------------------------------------------------
/mywebsite/settings.py:
--------------------------------------------------------------------------------
1 | """
2 | Django settings for mywebsite project.
3 |
4 | Generated by 'django-admin startproject' using Django 4.0.1.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/4.0/topics/settings/
8 |
9 | For the full list of settings and their values, see
10 | https://docs.djangoproject.com/en/4.0/ref/settings/
11 | """
12 |
13 | from pathlib import Path
14 |
15 | # Build paths inside the project like this: BASE_DIR / 'subdir'.
16 | BASE_DIR = Path(__file__).resolve().parent.parent
17 |
18 |
19 | # Quick-start development settings - unsuitable for production
20 | # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
21 |
22 | # SECURITY WARNING: keep the secret key used in production secret!
23 | SECRET_KEY = 'django-insecure-6vo^h26+(yy@p809%z-&+&-1)*n56uf(5q4fw6h&@f1yq@kp#l'
24 |
25 | # SECURITY WARNING: don't run with debug turned on in production!
26 | DEBUG = True
27 |
28 | ALLOWED_HOSTS = []
29 |
30 |
31 | # Application definition
32 |
33 | INSTALLED_APPS = [
34 | 'channels',
35 |
36 | 'django.contrib.admin',
37 | 'django.contrib.auth',
38 | 'django.contrib.contenttypes',
39 | 'django.contrib.sessions',
40 | 'django.contrib.messages',
41 | 'django.contrib.staticfiles',
42 |
43 | 'chat',
44 | ]
45 |
46 | ASGI_APPLICATION = 'mywebsite.asgi.application'
47 |
48 | CHANNEL_LAYERS = {
49 | 'default':{
50 | 'BACKEND':'channels.layers.InMemoryChannelLayer'
51 | }
52 | }
53 |
54 | MIDDLEWARE = [
55 | 'django.middleware.security.SecurityMiddleware',
56 | 'django.contrib.sessions.middleware.SessionMiddleware',
57 | 'django.middleware.common.CommonMiddleware',
58 | 'django.middleware.csrf.CsrfViewMiddleware',
59 | 'django.contrib.auth.middleware.AuthenticationMiddleware',
60 | 'django.contrib.messages.middleware.MessageMiddleware',
61 | 'django.middleware.clickjacking.XFrameOptionsMiddleware',
62 | ]
63 |
64 | ROOT_URLCONF = 'mywebsite.urls'
65 |
66 | TEMPLATES = [
67 | {
68 | 'BACKEND': 'django.template.backends.django.DjangoTemplates',
69 | 'DIRS': [],
70 | 'APP_DIRS': True,
71 | 'OPTIONS': {
72 | 'context_processors': [
73 | 'django.template.context_processors.debug',
74 | 'django.template.context_processors.request',
75 | 'django.contrib.auth.context_processors.auth',
76 | 'django.contrib.messages.context_processors.messages',
77 | ],
78 | },
79 | },
80 | ]
81 |
82 | WSGI_APPLICATION = 'mywebsite.wsgi.application'
83 |
84 |
85 | # Database
86 | # https://docs.djangoproject.com/en/4.0/ref/settings/#databases
87 |
88 | DATABASES = {
89 | 'default': {
90 | 'ENGINE': 'django.db.backends.sqlite3',
91 | 'NAME': BASE_DIR / 'db.sqlite3',
92 | }
93 | }
94 |
95 |
96 | # Password validation
97 | # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
98 |
99 | AUTH_PASSWORD_VALIDATORS = [
100 | {
101 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
102 | },
103 | {
104 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
105 | },
106 | {
107 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
108 | },
109 | {
110 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
111 | },
112 | ]
113 |
114 |
115 | # Internationalization
116 | # https://docs.djangoproject.com/en/4.0/topics/i18n/
117 |
118 | LANGUAGE_CODE = 'en-us'
119 |
120 | TIME_ZONE = 'UTC'
121 |
122 | USE_I18N = True
123 |
124 | USE_TZ = True
125 |
126 |
127 | # Static files (CSS, JavaScript, Images)
128 | # https://docs.djangoproject.com/en/4.0/howto/static-files/
129 |
130 | STATIC_URL = 'static/'
131 |
132 | # Default primary key field type
133 | # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
134 |
135 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
136 |
--------------------------------------------------------------------------------
/mywebsite/urls.py:
--------------------------------------------------------------------------------
1 | """mywebsite URL Configuration
2 |
3 | The `urlpatterns` list routes URLs to views. For more information please see:
4 | https://docs.djangoproject.com/en/4.0/topics/http/urls/
5 | Examples:
6 | Function views
7 | 1. Add an import: from my_app import views
8 | 2. Add a URL to urlpatterns: path('', views.home, name='home')
9 | Class-based views
10 | 1. Add an import: from other_app.views import Home
11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12 | Including another URLconf
13 | 1. Import the include() function: from django.urls import include, path
14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15 | """
16 | from django.contrib import admin
17 | from django.urls import path, include
18 |
19 | urlpatterns = [
20 | path('admin/', admin.site.urls),
21 | path('', include('chat.urls'))
22 | ]
23 |
--------------------------------------------------------------------------------
/mywebsite/wsgi.py:
--------------------------------------------------------------------------------
1 | """
2 | WSGI config for mywebsite project.
3 |
4 | It exposes the WSGI callable as a module-level variable named ``application``.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
8 | """
9 |
10 | import os
11 |
12 | from django.core.wsgi import get_wsgi_application
13 |
14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mywebsite.settings')
15 |
16 | application = get_wsgi_application()
17 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | asgiref==3.4.1
2 | attrs==21.4.0
3 | autobahn==21.11.1
4 | Automat==20.2.0
5 | backports.zoneinfo==0.2.1
6 | cffi==1.15.0
7 | channels==3.0.4
8 | constantly==15.1.0
9 | cryptography==36.0.1
10 | daphne==3.0.2
11 | Django==4.0.1
12 | hyperlink==21.0.0
13 | idna==3.3
14 | incremental==21.3.0
15 | pyasn1==0.4.8
16 | pyasn1-modules==0.2.8
17 | pycparser==2.21
18 | pyOpenSSL==21.0.0
19 | service-identity==21.1.0
20 | six==1.16.0
21 | sqlparse==0.4.2
22 | Twisted==21.7.0
23 | twisted-iocpsupport==1.0.2
24 | txaio==21.2.1
25 | typing_extensions==4.0.1
26 | tzdata==2021.5
27 | zope.interface==5.4.0
28 |
--------------------------------------------------------------------------------