Black Magic
Black Magic

Reputation: 2766

Django Channels not sending reply message

Let me preface this by saying: I am a beginner at RabbitMQ and the concepts around it. I think I am starting to understand though.

I currently have a problem with Django Channels. I had a setup where I was using the 'asgiref.inmemory.ChannelLayer' and I am currently switching to 'asgi_rabbitmq.RabbitmqChannelLayer' This all seems to work fine until I try to connect to the websocket from the browser. Nothing happens for a while (7 or 8 seconds) and then all of a sudden the websocket.receive gets triggered and the websocket disconnects.

I think it is because the reply channel is not receiving my accept message.(or perhaps too late).

routing:

lobby_routing = [
    route('websocket.connect', ws_test_add, path=r"^/testadd/$"),
]

Consumer:

def ws_test_add(message):
    print "TEST"
    message.reply_channel.send({'accept': True})
    Group("testadd").add(message.reply_channel)

Settings:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_rabbitmq.RabbitmqChannelLayer",
        "ROUTING": 'CouchGames_Backend.routing.lobby_routing',
        "CONFIG": {
            'url': 'amqp://guest:guest@localhost:5672/%2F',
        },
    },
}

Log:

2018-01-31 20:00:21,141 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-01-31 20:00:21,144 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-01-31 20:00:21,157 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-01-31 20:00:21,157 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-01-31 20:00:21,163 - INFO - server - HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2018-01-31 20:00:21,164 - INFO - server - Using busy-loop synchronous mode on channel layer
2018-01-31 20:00:21,164 - INFO - server - Listening on endpoint tcp:port=8000:interface=127.0.0.1
[2018/01/31 20:00:41] WebSocket HANDSHAKING /testadd/ [127.0.0.1:31404]
TEST
[2018/01/31 20:00:41] WebSocket DISCONNECT /testadd/ [127.0.0.1:31404]

Upvotes: 1

Views: 900

Answers (1)

Black Magic
Black Magic

Reputation: 2766

Turns out, it is propably because RabbitMQ is not meant for creating queues on the fly like that. So it is too slow causing the websocket to time out. I switched to a Redis channel layer and now it seems to be working perfectly!

Upvotes: 1

Related Questions