Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way Channels can read existing published messages from another app using Redis? #235

Closed
cdnsteve opened this issue Jul 7, 2016 · 1 comment

Comments

@cdnsteve
Copy link

cdnsteve commented Jul 7, 2016

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?

@andrewgodwin
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants