Channels: Daphne not working in production

Created on 13 Nov 2018  路  8Comments  路  Source: django/channels

Daphne not working properly in production

these are few files

Procfile

web: gunicorn django_analytics.wsgi --log-file -
web: daphne django_analytics.asgi:channel_layer --port $PORT --bind 0.0.0.0 -v2

settings.py

"""
Django settings for django_analytics project.

Generated by 'django-admin startproject' using Django 2.1.

For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(_file_)))


# print(STATICFILES_DIRS)

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'zrqi^g%%r%ac-tppuzt1#58xtxr!7^xi=s-^$nyzc65+snyn!o'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ["*"]


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'channels',
    'view_analytics',
    'analytics',
]

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',
    # 'analytics.middlewares.TimingMiddleware'

]

ROOT_URLCONF = 'django_analytics.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'django_analytics.wsgi.application'
ASGI_APPLICATION = 'django_analytics.routings.application'


CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer",
        "CONFIG": {
        },
    },
}
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_ROOT = "/app/view_analytics/static",
STATIC_URL = '/static/'



asgi.py

import os
import django
from channels.layers import get_channel_layer
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_analytics.settings")
django.setup()
channel_layer = get_channel_layer()
application = get_default_application()

This was the error log

2018-11-13T16:48:47.448224+00:00 app[web.1]: 2018-11-13 16:48:47,448 ERROR Traceback (most recent call last):
2018-11-13T16:48:47.448245+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/daphne/http_protocol.py", line 160, in process
2018-11-13T16:48:47.448247+00:00 app[web.1]: "server": self.server_addr,
2018-11-13T16:48:47.448249+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
2018-11-13T16:48:47.448251+00:00 app[web.1]: result = result.throwExceptionIntoGenerator(g)
2018-11-13T16:48:47.448253+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
2018-11-13T16:48:47.448254+00:00 app[web.1]: return g.throw(self.type, self.value, self.tb)
2018-11-13T16:48:47.448256+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/daphne/server.py", line 186, in create_application
2018-11-13T16:48:47.448258+00:00 app[web.1]: application_instance = yield deferToThread(self.application, scope=scope)
2018-11-13T16:48:47.448259+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/twisted/python/threadpool.py", line 250, in inContext
2018-11-13T16:48:47.448261+00:00 app[web.1]: result = inContext.theWork()
2018-11-13T16:48:47.448263+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/twisted/python/threadpool.py", line 266, in
2018-11-13T16:48:47.448264+00:00 app[web.1]: inContext.theWork = lambda: context.call(ctx, func, args, *kw)
2018-11-13T16:48:47.448266+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/twisted/python/context.py", line 122, in callWithContext
2018-11-13T16:48:47.448267+00:00 app[web.1]: return self.currentContext().callWithContext(ctx, func, args, *kw)
2018-11-13T16:48:47.448269+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/twisted/python/context.py", line 85, in callWithContext
2018-11-13T16:48:47.448270+00:00 app[web.1]: return func(args,*kw)
2018-11-13T16:48:47.448272+00:00 app[web.1]: TypeError: 'InMemoryChannelLayer' object is not callable

I am getting daphne internal server error 500

I also cant use redis :-(
what should I do

a similar error I found was
https://github.com/django/channels/issues/1035

Most helpful comment

Hey, I'm afraid this issue tracker is for specific, reproduceable bugs, not general support requests - see https://channels.readthedocs.io/en/latest/support.html for where you should ask those.

In particular, you shouldn't be setting an InMemoryChannelLayer for production usage, it's for testing only.

All 8 comments

Hey, I'm afraid this issue tracker is for specific, reproduceable bugs, not general support requests - see https://channels.readthedocs.io/en/latest/support.html for where you should ask those.

In particular, you shouldn't be setting an InMemoryChannelLayer for production usage, it's for testing only.

oh InMemoryChannelLayer is not supported?So is there any other layer apart from redis.
Sorry Sir for the inconvinience caused
Apart from redis can't I use any?

You don't need a channel layer, unless you want to talk between different consumers. But if you need one, Redis is the only one we maintain.

Ohkk thanks a lot for replying :-)

You don't need a channel layer, unless you want to talk between different consumers. But if you need one, Redis is the only one we maintain.

sir what is meant by between different consumers?Is there an example available that didn't use channel layer.at all?

You don't need a channel layer, unless you want to talk between different consumers. But if you need one, Redis is the only one we maintain.

yes sir I was also facing the same issue is there any eavailable example that dont use of channel layer

Is it possible to send a websocket to all the connected users without using a channel layer ?

Sure, but you need _something_ that lets you broadcast to all connected sockets that they are also all listening on. The channel layer is one way of doing this, but you could roll your own too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JanMalte picture JanMalte  路  3Comments

nefegago picture nefegago  路  4Comments

shitone picture shitone  路  3Comments

andrewfam picture andrewfam  路  3Comments

fakabbir picture fakabbir  路  5Comments