Django channels error:- Could not connect to ws://192.168.1.21:8000/ws/sc/,

IMO I think it is unable to find the route /ws/sc route but I dont understand why as I have linked it properly.

The error is give when I try to establish a connection using postman

Following is the postman error:-

Could not connect to ws://192.168.1.21:8000/ws/sc/
12:30:20
Error: Unexpected server response: 404
Handshake Details
Request URL: http://192.168.1.21:8000/ws/sc/
Request Method: GET
Status Code: 404 Not Found

Consumer class :-

from channels.consumer import AsyncConsumer, SyncConsumer

class MySyncConsumer(SyncConsumer):

def websocket_connect(self, event):
    print("Websocket Connected", event)
    self.send({
        'type': 'websocket.accept'
    })
    
def websocket_receive(self, event):
    print("Message recieved", event)

def websocket_disconnect(self, event):
    print("Websocket Disconnected", event)

Routing class

from django.urls import path, re_path
from . import consumers

websocket_urlpatterns = [
    re_path(r'ws/sc/$', consumers.MySyncConsumer.as_asgi()),
]

Asgi class

import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import core.routing


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

application = ProtocolTypeRouter(
    {
        "httpa": get_asgi_application(),
        "websocket": AuthMiddlewareStack(
        URLRouter(
            core.routing.websocket_urlpatterns
        )
    ),
    }
)

Settings.py

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-*3c_890(%*-no1e64qvxtq^1su(8-pyb5t46p-kzpqu=1173%i'

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

ALLOWED_HOSTS = []


# Application definition

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

    'core',
]

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

ROOT_URLCONF = 'gs3.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',
            ],
        },
    },
]

ASGI_APPLICATION = 'gs3.asgi.application'


# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/5.0/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/5.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


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

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

It is giving 404 error as it is unable to locate ws/sc path. I have uploaded all classes in which I made changes, I am new to learning websocket but I positive that I have setup it correctly but still it is not able to connect

There is typo in the Asgi Class. Changing the httpa to http should fix this?

ProtocolTypeRouter({
    "http": some_app,
    "websocket": some_other_app,
})
Back to Top