You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using Channels to power a real-time dashboard from Redis, where the data is published from another app. Trying to share messages between two apps, via Redis.
Architecture:
Redis (shared between two apps)
Flask, publisher
Django with Channels, subscriber
When the Flask app publishes a message onto Redis I'd like to be able to have Django Channels read that message from Redis.
Code to publish: current_app.redis.publish('dashboard', message)
I've tried setting the Django channel to dashboard as well but it doesn't seem to be getting messages in a way it expects.
How I managed to get it working:
In the Django app, I've created a worker.py file that subscribes to Redis channel dashboard.
It reads the message from Redis, then re-publishes it using Django Channels on a default channel.
Then the client side JS uses websockets get the messages/data in real-time.
I'd like to see if there's a better way of doing this. Can Django Channels be customized/configured to support getting data from an existing Redis instance, outside of Channels creating it, so I don't have to use this custom worker?
The text was updated successfully, but these errors were encountered:
Channels doesn't use Redis pub/sub, but instead Redis list keys with expires and some other stuff, and Lua running a lot of it for efficiency. It's not a format you're meant to be able to publish into directly in Redis, really.
In this scenario you have two options:
Publish directly to Channels from Flask. You can publish onto Channels from any Python code; either you import your project's asgi.py file and use the channel_layer object inside, which presents an interface matching the ASGI spec (see http://channels.readthedocs.io/en/latest/asgi.html); in particular, you can just do channel_layer.send(channel, message). If you don't want to import the Django project you can also import and set up an instance of RedisChannelLayer with matching settings directly too, from the asgi_redis package; everything in Channels is backed by a non-Django-specific thing.
As you have done, make a new process that bridges the two, receiving from one and subscribing to the other. This is commonly called an "interface server" in the docs; Daphne bridges HTTP and WebSockets into Channels, for example, so it would not be crazy to have something that bridged Redis pub/sub over too.
I'm using Channels to power a real-time dashboard from Redis, where the data is published from another app. Trying to share messages between two apps, via Redis.
Architecture:
When the Flask app publishes a message onto Redis I'd like to be able to have Django Channels read that message from Redis.
Code to publish:
current_app.redis.publish('dashboard', message)
I've tried setting the Django channel to
dashboard
as well but it doesn't seem to be getting messages in a way it expects.How I managed to get it working:
In the Django app, I've created a
worker.py
file that subscribes to Redis channeldashboard
.It reads the message from Redis, then re-publishes it using Django Channels on a
default
channel.Then the client side JS uses websockets get the messages/data in real-time.
I'd like to see if there's a better way of doing this. Can Django Channels be customized/configured to support getting data from an existing Redis instance, outside of Channels creating it, so I don't have to use this custom worker?
The text was updated successfully, but these errors were encountered: