Skip to content

Getting TypeError: SSEConsumer() missing 2 required positional arguments: 'receive' and 'send' #2105

@harkishankhuva

Description

@harkishankhuva

I've created an SSEconsumer class inheriting the AsyncHttpConsumer, when I called the endpoint it shows me an error TypeError: SSEConsumer() missing 2 required positional arguments: 'receive' and 'send'

Platform: Ubuntu 22.04 LTS
Python: 3.11.1

Output of pip freeze command

asgiref==3.8.1
attrs==23.2.0
autobahn==23.6.2
Automat==22.10.0
cffi==1.16.0
channels==4.1.0
constantly==23.10.4
cryptography==42.0.8
daphne==4.1.2
Django==5.0.6
hyperlink==21.0.0
idna==3.7
incremental==22.10.0
pyasn1==0.6.0
pyasn1_modules==0.4.0
pycparser==2.22
pyOpenSSL==24.1.0
service-identity==24.1.0
six==1.16.0
sqlparse==0.5.0
Twisted==24.3.0
txaio==23.1.1
typing_extensions==4.12.2
zope.interface==6.4.post2

I'm running the app using python manage.py runserver and daphne is added in 'INSTALLED_APPS' at the top.

Here's the full traceback

Traceback (most recent call last):
  File "/home/user1/temp/sse-testing/env/lib/python3.11/site-packages/asgiref/sync.py", line 518, in thread_handler
    raise exc_info[1]
  File "/home/user1/temp/sse-testing/env/lib/python3.11/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = await get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user1/temp/sse-testing/env/lib/python3.11/site-packages/asgiref/sync.py", line 518, in thread_handler
    raise exc_info[1]
  File "/home/user1/temp/sse-testing/env/lib/python3.11/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
    response = await wrapped_callback(
                     ^^^^^^^^^^^^^^^^^
TypeError: SSEConsumer() missing 2 required positional arguments: 'receive' and 'send'

FILE: consumers.py

import asyncio
from channels.generic.http import AsyncHttpConsumer
import datetime

class SSEConsumer(AsyncHttpConsumer):
    async def handle(self, body):
        await self.send_headers(headers=[
            (b"Cache-Control", b"no-cache"),
            (b"Content-Type", b"text/event-stream"),
            (b"Transfer-Encoding", b"chunked"),
        ])
        while True:
            payload = "data: %s\n\n" % datetime.now().isoformat()
            await self.send_body(payload.encode("utf-8"), more_body=True)
            await asyncio.sleep(1)

FILE: asgi.py

import os

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

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ssetesting.settings')
django_application = get_asgi_application()

application = ProtocolTypeRouter({
    "http": django_application
})

FILE: urls.py

from .import consumers
from django.urls import path

urlpatterns = [
    path('async-stream', consumers.SSEConsumer.as_asgi())
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions