mehdi
mehdi

Reputation: 1150

AsyncWebsocketConsumer VS AsyncConsumer

I'm trying to use Channels2 in my project. it's the first time that I meet channel in Django :) I have two main useful and almost complete sources here: 1)video on youtube DJANGO CHANNELS 2 Tutorial (V2) - Real Time 2)document of Channel in Read The Doc

as I don't know what will happen in the future of my code I need you to help me choose using between AsyncConsumer as mentioned in source #1, or AsyncWebsocketConsumer that is used in source # 2 for starting Django channel app that including by this way:

from channels.generic.websocket import AsyncWebsocketConsumer
from channels.consumer import AsyncConsumer

explanation:

class AsyncConsumer:
    """
    Base consumer class. Implements the ASGI application spec, and adds on
    channel layer management and routing of events to named methods based
    on their type.
    """

class AsyncWebsocketConsumer(AsyncConsumer):
    """
    Base WebSocket consumer, async version. Provides a general encapsulation
    for the WebSocket handling model that other applications can build on.
    """

my goal of using channel: trying to integrated real-time chat, notification/alert/transfer_data to the clients for the specific situations. (now the app working without Websocket with DRF)

and if you have any suggestions, ideas or notices I will be very happy to listen.thank you so much.

Upvotes: 2

Views: 3292

Answers (1)

Ken4scholars
Ken4scholars

Reputation: 6296

Channels is a project meant to be used to work with different protocols including but not limited to HTTP and WebSockets as explained on the docs page. The AsyncConsumer is the base generic consumer class from which other protocol-specific consumer classes are derived. One of those classes is the AsynWebsocketConsumer which you mentioned. As the name goes, it's used to work with websockets, so if you want use websockets for your realtime app, then that is the class you should use. There is also the AsyncHttpConsumer for working with HTTP. You most likely want to work with websockets so go with the AsynWebsocketConsumer or its derivative, AsyncJsonWebsocketConsumer.

I would also advise that you read the documentation to understand in better detail about the supported protocols and how and when to use them.

Upvotes: 4

Related Questions