channels: Recent version of channels doesn't support ASGI3

Since the recent version of daphne (2.3.0) added support for ASGI3, I no longer can’t use channels (2.2.0).

https://github.com/django/daphne/blob/master/daphne/cli.py#L30

Here it would likely to call channel’s router ( ProtocolTypeRouter) with additional two parameters (send, receive) which by now (https://github.com/django/channels/blob/2486738765064525cd8ee39294c44600140476ac/channels/routing.py#L56) is not supported.

Can we have support for ASGI3?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 8
  • Comments: 15 (10 by maintainers)

Most upvoted comments

N.B. you should be able to wrap Channels ASGI 2 apps with asgiref.compatibility.guarantee_single_callable - https://github.com/django/asgiref/blob/master/asgiref/compatibility.py#L39 - to convert it to ASGI 3.

As for now, I just created subclass of ProtocolTypeRouter like below to support ASGI3.

from channels.routing import ProtocolTypeRouter


class CustomSocketioProtocolTypeRouter(ProtocolTypeRouter):
    """
    Overrided base class's __call__ method to support python socketio 4.2.0 and daphne 2.3.0
    """
    def __call__(self, scope, *args):
        if scope["type"] in self.application_mapping:
            handlerobj = self.application_mapping[scope["type"]](scope)
            if args:
                return handlerobj(*args)
            return handlerobj
        raise ValueError(
            "No application configured for scope type %r" % scope["type"]
        )

application = CustomSocketioProtocolTypeRouter({
    # (http->django views is added by default)
})