Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

channels without channel layer or any other free hosting

I have a project in django 2.0 nad django-channlels 2.0 which I need to host I followed the documentation and I was able to run channels on localhost along with redis but when I hosted on pythonanywhere,it showed it doesnot support websocket, so then I hosted on heroku,but there they were asking for verification of credit card info which i dont have to run redis.Are there additional hosting website whre I can rrun redis erver for free

Or is it poosible to implement channels without channel_layer and redis.My code is working perfectly fine on local host but can't host online for free.

class PageConsumer(WebsocketConsumer):
    def connect(self, **kwargs):
        self.accept()
        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_send)("admin", {"type": "analytics.admin_message", "message": "plus"})

    def disconnect(self, close_code):
        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_send)("admin", {"type": "analytics.admin_message", "message": "minus"})

its corresponidng receiver

class ChatConsumer(WebsocketConsumer):
    def connect(self, **kwargs):
        self.accept()
        async_to_sync(self.channel_layer.group_add)("admin", self.channel_name)

    def disconnect(self, close_code):
        async_to_sync(self.channel_layer.group_discard)("admin", self.channel_name)

    def analytics_admin_message(self, something):
        if something["message"] == "plus":
            self.send(text_data=json.dumps({
                'message': "plus"
            }))

        else:
            self.send(text_data=json.dumps({
                'message': "minus"
            }))

    def receive(self, text_data):
        print("data hai bhyi", text_data)
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        self.send(text_data=json.dumps({
            'message': message
        }))

settings.py

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("127.0.0.1", 6379)],
        },
    },
}
like image 547
Nimish Bansal Avatar asked Dec 07 '22 13:12

Nimish Bansal


1 Answers

from the docs

Channel layers are an entirely optional part of Channels as of version 2.0. If you don’t want to use them, just leave CHANNEL_LAYERS unset, or set it to the empty dict {}.

It will mean you will be unable to use self.channel_layer in the consumer, which you rely on.

So, it's optional but you need it.

In memory exists:

CHANNEL_LAYERS={
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer"
     }
}
like image 63
Django Doctor Avatar answered Dec 28 '22 10:12

Django Doctor