[Solved] group_send does not working
See original GitHub issue- OS : Linux Mint 19
- using docker-compose -> click HERE for .yml file
- using Django 2.0, Channels 2.1.2 , Channels-redis 2.2.1
- i’m using runserver to run everything
Routing.py :
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
from gce_app.consumers import MainConsumer
application = ProtocolTypeRouter({
# Empty for now (http->django views is added by default)
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter([
path("", MainConsumer)
])
)
),
})
Consumers.py :
from channels.generic.websocket import AsyncJsonWebsocketConsumer
class MainConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add('connectedUsers', self.channel_name)
print('connected')
async def disconnect(self, close_code):
await self.channel_layer.group_discard('connectedUsers',self.channel_name)
print('disconnected')
async def receive(self, text_data=None, bytes_data=None ):
print('received')
await self.channel_layer.group_send(
'connectedUsers', {
'type': 'connectedUsers.notify',
'content': 'this is a group message'
})
await self.send_json({
'content': 'this is a message for the one making the call'
})
async def connectedUsers_notify(self, event):
self.send(event)
asgi.py :
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
django.setup()
application = get_default_application()
settings channels config :
ASGI_APPLICATION = "gce.routing.application"
### Channel Redis Host
redis_host = os.environ.get('REDIS_HOST', 'redis')
# Channel layer definitions
# http://channels.readthedocs.org/en/latest/deploying.html#setting-up-a-channel-backend
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(redis_host, 6379)],
},
},
}
as you can see everyhting is correct (i think, from tutorial, i’m new) this works, i receive a message in the client sending a call to the consumer
await self.send_json({
'content': 'message for the user who we received a call from'
})
but this doesn’t, i don’t receive anything, in any client …
await self.channel_layer.group_send(
'events', {
'type': 'events.alarm',
'content': 'This is sent to all users connected using socket'
})
HELP please, i’m new to channels, i’m afraid this is not a bug but i’m doing something wrong, the channels documentation is very obscure.
this is my js :
var ws;
function init_socker_connection(){
ws = new WebSocket("ws:/localhost:8000");
ws.onopen = e => {
console.log('Open ', e);
}
ws.onmessage = e => {
console.log('Message ', e);
}
ws.onerror = e => {
console.log('Error ', e);
}
ws.onoclose = e => {
console.log('Close ', e);
}
}
function send_test() {
ws.send("Hello");
}
document.addEventListener('DOMContentLoaded', () => {
init_socker_connection();
}, false)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Django channels group_send not working properly
Basically, change from this: await self.channel_layer.group_send( "bidding", { "type": "update.price" "price": price, "item_id": item_id } ).
Read more >self.channel_layer.group_send not calling function-django
Channels provides a simple way of doing this if your group names are static there is a property groups on the instance you...
Read more >Channel Layers — Channels 4.0.0 documentation
In-memory channel layers operate with each process as a separate layer. This means that no cross-process messaging is possible. As the core value...
Read more >Fix problems signing in & viewing posts - Google Groups Help
Fix problems signing in & viewing posts. If you're signed in to a work or school account, the options you see might be...
Read more >365 group - send copy to sender? - Spiceworks Community
Solution : Answered my own question :Having connected to Office365 ... but I'm not the user - I'm the admin - is there...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
UPDATE: working
Changed to AsyncWebsocketConsumer and made sure all the intermittent calls were made async and awaited properly.
Same behaviour with channels 3 with .asgi or otherwise.
Thanks for all the help
I updated the channels version to 3.0.3, and added .as_asgi() to my routing file
Still not getting any ws messages on the client, using websocketking for testing