GithubHelp home page GithubHelp logo

dibs-devs / chatter Goto Github PK

View Code? Open in Web Editor NEW
84.0 13.0 29.0 214 KB

A simple Chat App for Django based on channels and websockets

License: MIT License

Python 60.25% HTML 11.55% JavaScript 20.52% CSS 7.18% Shell 0.49%

chatter's Introduction

Django Chatter

https://coveralls.io/repos/github/dibs-devs/chatter/badge.svg?branch=master https://travis-ci.org/dibs-devs/chatter.svg?branch=master https://pepy.tech/badge/django-chatter/month

Re-usable Django chat application for Django developers.

Full docs here: Django Chatter Docs

Chat is a crucial aspect of many web apps at present. However, Django's package repository does not have well-maintained reusable chat packages that Django developers can integrate into their platforms.

Django Chatter is an attempt to change that. This is an open-source fully reusable chat application that has mechanisms to support group chats in place.

The HTML front-end for this app is built with Flexbox, making it responsive to numerous viewports.

[More work to be done] Added to that, it can also possibly be used as a REST API, since all the views generate standard JSON responses that need to be parsed by the websockets present in the front-end of the app using this package.

This app makes use of Django Channels 2 and uses Redis as the message broker.

To run Django Chatter properly, you'll require python>=3.5 and Redis. Note: For development, we are currently using redis-5.0.3, built from source on Ubuntu machines.

The core mechanisms of Chatter follow the instructions provided in the Django Channels tutorial section, with some added modifications and theming.

Installation

  • Chatter is on PyPi now! To install it, run

    pip install django-chatter

    This should install all the required dependencies for Chatter.

  • Once you're done with that, add it to your settings.INSTALLED_APPS:

    INSTALLED_APPS = [
      ...
      'django_chatter',
      ...
      ]
  • Since we use Redis as our message broker, you need to enable channel layers for Chatter's ChatConsumer (see Channels' Consumers for more details). To enable that, you need to add the following lines to your project's settings.py file:

    CHANNEL_LAYERS = {
      'default': {
          'BACKEND': 'channels_redis.core.RedisChannelLayer',
          'CONFIG': {
              "hosts": [('127.0.0.1', 6379)],
          },
      },
    }
  • If you haven't already, create a file named routing.py in your project's configuration folder. This is because Django Channels uses a specification called ASGI for its websocket protocol. To enable Channels on your app, you have to add a file that routes all websocket requests to a Channels app (in this case, Chatter). This should be the same as the folder where your settings.py file is located.

    In routing.py, add the following lines:

    from channels.auth import AuthMiddlewareStack
    from channels.routing import ProtocolTypeRouter, URLRouter
    import django_chatter.routing
    
    application = ProtocolTypeRouter({
      'websocket': AuthMiddlewareStack(
        URLRouter(
        django_chatter.routing.websocket_urlpatterns # send websocket requests to chatter's urls
        )
      )
    })

    This routes all websocket requests to Chatter, with the logged in User object. If you are using different django-channels applications other than Chatter, you may already have this file, and can add the appropriate URL for chatter to handle. More details can be found on Django Channels' Routing page.

    If you know how the middleware wrapping in Channels works, then feel free to replace AuthMiddlewareStack with what you use as your auth middleware for User object processing (if you're curious to know about this, get in touch! We'd be happy to talk to you about it).

  • Now that you're done setting up routing.py, add the following line in your settings.py file to link to the routing.py (again, you may have already done this if you're already using channels)

    ASGI_APPLICATION = '<project name>.routing.application'
  • Chatter uses a context processor to generate a list of all rooms that a user is a member of. To use this context processor, add it to your TEMPLATES list in your settings.py file:

    TEMPLATES = [
      {
        ...
        'OPTIONS': {
          'context_processors': [
            ...,
            'django_chatter.context_processors.get_chatroom_list',
            ...,
          ],
        },
      },
    ]
  • Link django_chatter.urls to the URL you want in your URLConf (<project>/urls.py).

    Example:

    from django.urls import path, include
    
    ...
    urlpatterns = [
      ...,
      path('chat/', include('django_chatter.urls')),
      ...
    ]
  • Run migrations:

    $ python manage.py makemigrations django_chatter
    $ python manage.py migrate
  • Start your app's development server and go to your '/chat/' URL, and you will see Chatter's homepage.

Tests haven't been setup for this package yet. I built this app before I knew what good test practices were like. So, tests welcome!

Usage Notes

  • Chatter, as of right now, provides a very minimal interface for users to chat with other users.For starters, while group chatting is supported on the model layer, the corresponding templates and front-end logic have not yet been setup.
  • If you're using chatter as a package in your own app, you have to make sure that you handle user authentication in your app. Chatter, by default, provides views that require user authentication. If you're developing Chatter on the other hand, the usage will vary a bit. The notes for that can be found in the Get Involved section.

Running list of features to add

  • Add a "Create Group" option for users on the templates
  • Add 'Seen by user x' functionality

chatter's People

Contributors

dibs-devs avatar ishtiaque06 avatar kkruto avatar naveenslog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chatter's Issues

Let parent app users define a base.html file for chatter to inherit from

The problem: When chatter is being used by a different app as a reusable package, there needs to be a way for the developer to specify which base template they want chatter templates to inherit from. For example, developers might want a unique HTML header navbar on top of chatter. In this scenario, they will end up needing to copy and paste whole existing chatter templates just to add that navbar which appears all throughout their application.

The solution: Give developers an option to specify a base template chatter can inherit from. This can be specified in their app's settings.py file as CHATTER_BASE_TEMPLATE. When this variable is specified, chatter will sit inside the wrapped template provided by the parent application. Otherwise, it will just use the usual base.html template.

shortUUID bug in models.py

In chat/models.py, the Room's id is defaulted to shortuuid.ShortUUID.random(length=22). Should be shortuuid.ShortUUID().random.

"User is typing..." message

When a user starts typing on the chat, show that update to the other user(s) that are online in the chatroom.

This can be done by sending a message down the chatSocket.js object and echoing it out on the other end. Since each message sent through chatSocket object has an associated message_type string, this can be used to filter what type of message is being sent to the channels consumer for it to process and echo it back into the channel layer.

Immediately direct to the latest chat on homepage access

The problem: right now, the chatter homepage shows a generic webpage containing a list of all the rooms and a "Select a room to get started" message. However, to reflect how chat interfaces usually work, chatter should redirect users to the latest chat that's been active.

The solution: When a user hits the index view, decide if the user has any rooms they belong to. If so, return the chatroom view with the UUID of the latest room.

Multitenancy support for ChatConsumer

For now, ChatConsumer works for databases which don't use multitenancy with django-tenants and/or django-tenants-schemas. This needs to be changed to reflect Dibs' implementation of multitenancy, but will most probably be designed to be used with any multitenant apps.

No module named 'django_chatter.context_processors'

I was really excited to see this package but this didn't worked for me. I followed all the basic steps in read me. And when i hit runserver and moved to http://127.0.0.1:8000/chat . This show me an error :-

`ModuleNotFoundError at /chat/chat/2432b838-e7a2-4913-9c4e-b3bb61023fde/
No module named 'django_chatter.context_processors'

**`ModuleNotFoundError at /chat/chat/2432b838-e7a2-4913-9c4e-b3bb61023fde/
No module named 'django_chatter.context_processors'**

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/chat/chat/2432b838-e7a2-4913-9c4e-b3bb61023fde/

Django Version: 2.2.10
Python Version: 3.7.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'messaging',
 'rest_framework',
 'django_chatter']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "D:\Anaconda\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "D:\Anaconda\lib\site-packages\django\core\handlers\base.py" in _get_response
  145.                 response = self.process_exception_by_middleware(e, request)

File "D:\Anaconda\lib\site-packages\django\core\handlers\base.py" in _get_response
  143.                 response = response.render()

File "D:\Anaconda\lib\site-packages\django\template\response.py" in render
  106.             self.content = self.rendered_content

File "D:\Anaconda\lib\site-packages\django\template\response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "D:\Anaconda\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "D:\Anaconda\lib\site-packages\django\template\base.py" in render
  169.                 with context.bind_template(self):

File "D:\Anaconda\lib\contextlib.py" in __enter__
  112.             return next(self.gen)

File "D:\Anaconda\lib\site-packages\django\template\context.py" in bind_template
  242.         processors = (template.engine.template_context_processors +

File "D:\Anaconda\lib\site-packages\django\utils\functional.py" in __get__
  80.         res = instance.__dict__[self.name] = self.func(instance)

File "D:\Anaconda\lib\site-packages\django\template\engine.py" in template_context_processors
  85.         return tuple(import_string(path) for path in context_processors)

File "D:\Anaconda\lib\site-packages\django\template\engine.py" in <genexpr>
  85.         return tuple(import_string(path) for path in context_processors)

File "D:\Anaconda\lib\site-packages\django\utils\module_loading.py" in import_string
  17.     module = import_module(module_path)

File "D:\Anaconda\lib\importlib\__init__.py" in import_module
  127.     return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>" in _gcd_import
  1006. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load
  983. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
  965. <source code not available>

Exception Type: ModuleNotFoundError at /chat/chat/2432b838-e7a2-4913-9c4e-b3bb61023fde/
Exception Value: No module named 'django_chatter.context_processors'`

Please fix it or tell me a temporary solution ASAP.

Signals for parent apps to use

When it comes to real-time chatting, along comes the idea of real-time notifications as well. On the chatter level, this would involve notifying the app that's using chatter of a new message sent in the system by a user, so that if the parent app wants, it can use the signal to send a notification to the user using the app.

Infinite scroll on chatroom list

Since on first load, chatter only loads the latest 10 chatrooms, allowing an infinite scroll functionality would be great to have.

Conflicting requirements

You require explicit versions of channels and channels-redis, which are incompatible.
It makes this package kind of not installable.

$ cat requirements.in
django-chatter
$ pip-compile --upgrade --no-index --output-file=requirements.txt requirements.in
Could not find a version that matches asgiref~=2.1,~=2.3,~=3.0 (from channels==2.1.7->django-chatter==1.0.7->-r requirements.in (line 1))
Tried: 0.8, 0.9, 0.9.1, 0.10.0, 0.11.0, 0.11.0, 0.11.0, 0.11.1, 0.11.1, 0.11.2, 0.11.2, 0.12.0, 0.12.0, 0.12.1, 0.12.1, 0.13.0, 0.13.0, 0.13.2, 0.13.2, 0.13.3, 0.14.0, 0.14.0, 1.0.0, 1.0    There are incompatible versions in the resolved dependencies:
  asgiref~=2.1 (from channels-redis==2.3.3->django-chatter==1.0.7->-r requirements.in (line 1))
  asgiref~=2.3 (from channels==2.1.7->django-chatter==1.0.7->-r requirements.in (line 1))
  asgiref~=3.0 (from daphne==2.3.0->channels==2.1.7->django-chatter==1.0.7->-r requirements.in (line 1))

Scalability concern: Context processor

If a developer is using the context_processors.py provided by Chatter, it loads the list of all rooms a logged in user belongs to with every request. This can lead to additional overhead on page load and database querying, which may not be something that all developers want. This would only be relevant if the developer wants chat features on every page.

First stab: make the list of rooms available in the chatroom and index views as well as in the context_processor so that the developer can choose which one to use depending on the implementation they want.

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.