Berry
Berry

Reputation: 2318

Django Channels Group.send not working in python console?

I tried Group(groupname).send in the python console and it does not seem to work. Why is this?

This is my consumers.py arrangement:

def ws_connect(message):
    message.reply_channel.send({"accept": True})
    Group(secure_group).add(message.reply_channel)


def ws_receive(message):
    # Nothing to do here
    Group(secure_group).send({
        "text": "Received {}".format(message.content['text'])
    })


def ws_disconnect(message):
    Group(secure_group).discard(message.reply_channel)

Routing:

from channels.routing import route
from App.consumers import (
    ws_connect,
    ws_receive,
    ws_disconnect
)

channel_routing = [
    route("websocket.connect", ws_connect),
    route("websocket.receive", ws_receive),
    route("websocket.disconnect", ws_disconnect),
]

Terminal commands:

from channels import Group
#import secure_group here

Group(secure_group).send({ "text": "Tester" })

All my clients have never recieved the text.

CHANNEL_LAYERS config:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgiref.inmemory.ChannelLayer",
        "ROUTING": "App.routing.channel_routing",
    },
}

Upvotes: 2

Views: 768

Answers (1)

Raja Simon
Raja Simon

Reputation: 10315

Inmemory channel layer doesn't support cross-process communication. You can't perform Group send in other say terminal. Try with Redis backend you can send message.

From the doc In-Memory

Upvotes: 3

Related Questions