Daphne server shuts down after startup

I have a Django Rest Framework application with Daphne, I have ASGI server configured.
When starting the server everything seems fine, but after a few seconds it stops, without providing the reason why it is stopping.
What could be the problem?

Llogs:

2023-09-25 20:34:29,645 INFO     Starting server at tcp:port=8000:interface=127.0.0.1
2023-09-25 20:34:29,646 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2023-09-25 20:34:29,646 INFO     Configuring endpoint tcp:port=8000:interface=127.0.0.1
2023-09-25 20:34:29,647 INFO     Listening on TCP address 127.0.0.1:8000
2023-09-25 20:35:33,731 INFO     Killed 0 pending application instances

asgi.py:

import os
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application

default_asgi_application = get_asgi_application()

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'simple_chat.settings')

application = ProtocolTypeRouter({
  "http": default_asgi_application,
})

settings.py:

from os import environ
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = environ.get('DJANGO_SECRET_KEY')
DEBUG = True

ALLOWED_HOSTS = [
  'localhost',
  'simple-chat-api-dagamdev.koyeb.app'
]

INSTALLED_APPS = [
    'channels',
    'chat.apps.ChatConfig',
    'corsheaders',
    'rest_framework',
    'rest_framework.authtoken',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

AUTH_USER_MODEL = 'chat.CustomUser'

ROOT_URLCONF = 'simple_chat.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 = 'simple_chat.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': environ.get('ENGINE'),
        'NAME': environ.get('NAME'),
        'USER': environ.get('USER'),
        'PASSWORD': environ.get('PASSWORD'),
        'HOST': environ.get('HOST'),
        'PORT': environ.get('POSTGRESQL_PORT'),
    }
}


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',
    },
]


LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True


STATIC_URL = 'static/'
ASGI_APPLICATION = 'simple_chat.asgi.application'

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            'hosts': [environ.get('REDIS_URL')]
        },
    },
}


CORS_ALLOWED_ORIGINS = [
  'http://localhost:3000' if environ.get('IN_DEVELOPMENT') else 'https://globalpage.com'
]

Hello, which kind of instances your service is running on? If it is a nano instance, did you try running your service from a bigger instance?

I suspect 256MB of ram is not enough to host your Python application, and that’s why your process is getting killed.

Yes, I was using the nano instance, but it seems to me that it is not a question of the instance, I already tried the Micro and Small instance and the same thing still happens.
Also, I previously deployed my application but with the WSGI server and with the Nano instance it worked without a problem, but now that I try to deploy as an ASGI server it gives me that error.

Maybe I should configure my project as if I were deploying it to Heroku?

I found the solution, which is to configure the project as if you were deploying it to Heroku.

Add 2 files, the Procfile and runtime.txt file

Procfile content:

release: python manage.py migrate
web:daphne simple_chat.asgi:application --port $PORT --bind 0.0.0.0 -v2
worker: python manage.py runworker channels --settings=simple_chat.settings -v2

runtime.txt content:
version of python you used

python-3.11.0
1 Like