Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Yogendra0Sharma/b5e1a85eb02d1f4ed1b0c7df1248caf7 to your computer and use it in GitHub Desktop.

Select an option

Save Yogendra0Sharma/b5e1a85eb02d1f4ed1b0c7df1248caf7 to your computer and use it in GitHub Desktop.

install redis server

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-redis

  • add in requirements.txt
sudo pip install redis
  • or from scritp.sh
echo "Installing Redis..."
if [ ! -x /usr/local/bin/redis-server ]; then
  apt-get install -y build-essential tcl8.5

  wget http://download.redis.io/releases/redis-stable.tar.gz
  tar xzf redis-stable.tar.gz
  cd redis-stable

  make
  make install

  cd utils
  ./install_server.sh
fi

install dependencies (vagrant provision)

  • channels
  • pyhaikunator
  • asgi_redis

add channels to INSTALL_APPS and configuration to settings

INSTALLED_APPS = (
    '....',
    'channels',
    '....',
)

# DJANGO CHANNELS CONFIGURATION

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [os.environ.get('REDIS_URL', 'redis://127.0.0.1:6379')],
        },
        "ROUTING": "miproject.miapp.routing.channel_routing",
    },
}

create the asgi.py file in wsgi.py level(recommended) or in wsgi

# /asgi.py
import os
import channels.asgi

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "geventexample.settings")
channel_layer = channels.asgi.get_channel_layer()

# /wsgi.py
import os

from django.core.wsgi import get_wsgi_application

import channels.asgi # noqa


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tandlr.settings")

application = get_wsgi_application()
channel_layer = channels.asgi.get_channel_layer()

Add consumers file

# -*- coding: utf-8 -*-
import logging

from channels import Group
from channels.sessions import channel_session

log = logging.getLogger(__name__)


@channel_session
def ws_connect(message):
    Group('chat')


@channel_session
def ws_receive(message):
    pass


@channel_session
def ws_disconnect(message):
    pass

Add routing file

# -*- coding: utf-8 -*-

from channels.staticfiles import StaticFilesConsumer

from tandlr.notifications import consumers

channel_routing = {
    'http.request': StaticFilesConsumer(),

    # Wire up websocket channels to our consumers:
    'websocket.connect': consumers.ws_connect,
    'websocket.receive': consumers.ws_receive,
    'websocket.disconnect': consumers.ws_disconnect,
}

Add function in fabfile.py

@task
def runworker():
    """
    Starts the development worker inside the Vagrant VM.
    """
    with virtualenv():
        run('python manage.py runworker')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment