Skip to content Skip to sidebar Skip to footer

Reusing An Existing Websocket In Django Channels

I'm messing around semi-seriously with Python to create a sort of a gatekeeper server between a restricted-access system and a GAE application. I'm going to start with a more gener

Solution 1:

Maybe already be too late, but what you need to do is send a message directly to the channel or group the application is listening to. Channels will route your message to the appropriate consumer method.

So assuming you want to call the send_message method in the consumer serving your other app(say channel1): you should do this:

from channels.layersimport get_channel_layer
from asgiref.syncimport async_to_sync

channel_layer = get_channel_layer()
sync_to_async(channel_layer.send)("channel1", {
    "type": "send.message",
    "text": "Hello there!",
})

This can be done, anywhere in your code.

The question you linked uses tornado websockets which is quite different from Django channels. I don't know if it is possiblle using tornado but it definitely is, using Channels

Post a Comment for "Reusing An Existing Websocket In Django Channels"