Skip to main content

Redis-backed ASGI channel layer implementation

Project description

https://api.travis-ci.org/django/channels_redis.svg https://img.shields.io/pypi/v/channels_redis.svg

A Django Channels channel layer that uses Redis as its backing store, and supports both a single-server and sharded configurations, as well as group support.

Note: Prior versions of this package were called asgi_redis and are still available under PyPI as that name if you need them for Channels 1.x projects. This package is for Channels 2 projects only.

Usage

Set up the channel layer in your Django settings file like so:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
        },
    },
}

Possible options for CONFIG are listed below.

hosts

The server(s) to connect to, as either URIs, (host, port) tuples, or dicts conforming to create_connection. Defaults to ['localhost', 6379]. Pass multiple hosts to enable sharding, but note that changing the host list will lose some sharded data.

prefix

Prefix to add to all Redis keys. Defaults to asgi:. If you’re running two or more entirely separate channel layers through the same Redis instance, make sure they have different prefixes. All servers talking to the same layer should have the same prefix, though.

expiry

Message expiry in seconds. Defaults to 60. You generally shouldn’t need to change this, but you may want to turn it down if you have peaky traffic you wish to drop, or up if you have peaky traffic you want to backlog until you get to it.

group_expiry

Group expiry in seconds. Defaults to 86400. Channels will be removed from the group after this amount of time; it’s recommended you reduce it for a healthier system that encourages disconnections. This value should not be lower than the relevant timeouts in the interface server (e.g. the --websocket_timeout to daphne).

capacity

Default channel capacity. Defaults to 100. Once a channel is at capacity, it will refuse more messages. How this affects different parts of the system varies; a HTTP server will refuse connections, for example, while Django sending a response will just wait until there’s space.

channel_capacity

Per-channel capacity configuration. This lets you tweak the channel capacity based on the channel name, and supports both globbing and regular expressions.

It should be a dict mapping channel name pattern to desired capacity; if the dict key is a string, it’s intepreted as a glob, while if it’s a compiled re object, it’s treated as a regular expression.

This example sets http.request to 200, all http.response! channels to 10, and all websocket.send! channels to 20:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
            "channel_capacity": {
                "http.request": 200,
                "http.response!*": 10,
                re.compile(r"^websocket.send\!.+"): 20,
            },
        },
    },
}

If you want to enforce a matching order, use an OrderedDict as the argument; channels will then be matched in the order the dict provides them.

symmetric_encryption_keys

Pass this to enable the optional symmetric encryption mode of the backend. To use it, make sure you have the cryptography package installed, or specify the cryptography extra when you install channels_redis:

pip install channels_redis[cryptography]

symmetric_encryption_keys should be a list of strings, with each string being an encryption key. The first key is always used for encryption; all are considered for decryption, so you can rotate keys without downtime - just add a new key at the start and move the old one down, then remove the old one after the message expiry time has passed.

Data is encrypted both on the wire and at rest in Redis, though we advise you also route your Redis connections over TLS for higher security; the Redis protocol is still unencrypted, and the channel and group key names could potentially contain metadata patterns of use to attackers.

Keys should have at least 32 bytes of entropy - they are passed through the SHA256 hash function before being used as an encryption key. Any string will work, but the shorter the string, the easier the encryption is to break.

If you’re using Django, you may also wish to set this to your site’s SECRET_KEY setting via the CHANNEL_LAYERS setting:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": ["redis://:password@127.0.0.1:6379/0"],
            "symmetric_encryption_keys": [SECRET_KEY],
        },
    },
}

Dependencies

Redis >= 2.6 is required for channels_redis. It supports Python 3.5.2 and up (3.5.0 or 3.5.1 will not work due to our dependency, aioredis).

Contributing

Please refer to the main Channels contributing docs. That also contains advice on how to set up the development environment and run the tests.

Maintenance and Security

To report security issues, please contact security@djangoproject.com. For GPG signatures and more security process information, see https://docs.djangoproject.com/en/dev/internals/security/.

To report bugs or request new features, please open a new GitHub issue.

This repository is part of the Channels project. For the shepherd and maintenance team, please see the main Channels readme.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

channels_redis-2.3.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

channels_redis-2.3.0-py2.py3-none-any.whl (11.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file channels_redis-2.3.0.tar.gz.

File metadata

  • Download URL: channels_redis-2.3.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.5.6

File hashes

Hashes for channels_redis-2.3.0.tar.gz
Algorithm Hash digest
SHA256 1b89421285dacd462bec884d537d796abf5b1019e9494e5258d54c2cf14840b3
MD5 df9fb5436baadcd0bb9c48022d10e521
BLAKE2b-256 d3c94414982e46df34ba1dc7b4be44c226793b94fc9e577ec40573daf0bc342f

See more details on using hashes here.

File details

Details for the file channels_redis-2.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: channels_redis-2.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.5.6

File hashes

Hashes for channels_redis-2.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 30e99156db0158d74c7dc3a8d1c1d39e0e62c68d2ba1677bc11022579a58df14
MD5 5d744e425a2f2c19ed62163ca7d0e967
BLAKE2b-256 ba218ac93e9cf9d97c3f2256ccc04d992b6b10de6b3d9c62a5f3c49a89df6510

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page