3

I've been trying to get my head around the channels of Django and I can't get my message to be sent to my websocket.

Here is my consumers.py

import logging
from django.contrib.sites.models import Site
from django.utils import timezone
from channels import Group
from .models import *
import json

def send_free(message):
    try:
        pi = PInformation.objects.get(
            pk=message.content.get('pk'),
        )
    except Parkplatzinformationen.DoesNotExist:
        logging.error("PI not found!")
        return

    try:
        message.reply_channel.send({
        "text": 1,
    })
    except:
        logging.exception('Problem sending %s' % (pi.name))

My routing.py

from channels.routing import route

from RESTAPI.consumers import send_free

channel_routing = [
    route('send-free',send_free),
]

I'm getting the error AttributeError: 'NoneType' object has no attribute 'send'. It does however get the PInformation object so it does work a "bit". I'm calling it right after I'm saving the object.

Could you please give me some hints? The Getting Started guide uses it like I try to.

1 Answer 1

1

I assume you are calling "send-free" from your view like this...

Channel('send-free').send({'message': 'your message'})

Then send_free doesn't have the message.reply_channel...

In other word once the WebSocket packet is sent to us by a client then message takes the reply_channel attribute from it. That will use to reply message back to client... ( to frontend maybe )

So do you really want to send message...? then again send using consumers...

Sign up to request clarification or add additional context in comments.

6 Comments

Yes you are correct I'm calling the send-free the way you described it. However I do not get what do you mean by repeating the process.
@dowu Repeat Channel("cons").send(dict) and also I edited the answer..
Okay so I call Channel("otherchanel").send(dict) instead of message.reply_channel.send again? Isn't "otherchannel" supposed to be linked to a function and so I'm stuck with the same problem?
What message your are trying to send back and why ? You are calling the consumer function from your view then you are expecting your consumer to send back the message back to view ?
I want to send a dict to the client after the object has been updated so I can display that information on a homepage (retrieving it with javascript). (thank you for your patience)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.