Skip to main content

PostgreSQL-backed ASGI channel layer implementation

Project description

channels_postgres

Tests channels_postgres pypi

A Django Channels channel layer that uses PostgreSQL as its backing store

Installation

pip install channels_postgres

Update INSTALLED_APPS

INSTALLED_APPS = (
    ...
    'channels',
    'channels_postgres',
    ...
)

Run migrations for internal tables

python manage.py migrate channels_postgres

Update DATABASES dictionary

Add the 'channels_postgres' entry to the DATABASES dictionary in your Django settings file like so:

DATABASES = {
	'default': {
		...
	},
	'channels_postgres': {
		'ENGINE': 'django.db.backends.postgresql_psycopg2',
		'NAME': 'postgres',
		'USER': 'postgres',
		'PASSWORD': 'password',
		'HOST': '127.0.0.1',
		'PORT': '5432',
	}
}

Usage

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

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_postgres.core.PostgresChannelLayer',
        'CONFIG': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'postgres',
            'USER': 'postgres',
            'PASSWORD': 'password',
            'HOST': '127.0.0.1',
            'PORT': '5432',

            'config: {
                ...
            }
        },
    },
}

The Config object is exactly the same as the standard config object for Django's PostgreSQL database. See the django documentation for more information.

config is a dictionary of parameters to the underlying async postgres library (in this case aiopg) This setting can be used to control the database pool size, connection timeout etc. See the aiopg documentation for more information.

A typical use of config would be to increase the maxsize of the connection pool. The default of 10 might be too low for sites with a decent amount of traffic.

The config parameters are described below:

prefix

Prefix to add to all database group keys. Defaults to asgi:. In most cases, you don't need to change this setting because it's only used internally.

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

Defaults to 0.

0 means disabled!

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

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_postgres:

pip install channels_postgres[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 Postgres, though we advise you also route your Postgres connections over TLS for higher security.

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_postgres.core.PostgresChannelLayer',
        'CONFIG': {
            ...,
            'symmetric_encryption_keys': [SECRET_KEY],
        },
    },
}

Deviations from the channels spec

group_expiry

Defaults to 0 (which means disabled). This option is tied too closely to daphne (The official ASGI interface server for django-channels). It makes no sense if you're using an alternate ASGI server (like Uvicorn) which doesn't disconnect WebSockets automatically.

Setting it to a non zero value enables the expected behaviour.

channel_capacity

RDMS' like PostgreSQL were specifically built to handle huge amounts of data without crashing down and using too much memory. Hence, there's no channel capacity.

Your database should be able to handle thousands of messages with ease. If you're still worried about the database table growing out of hand, you can reduce the expiry time of the individual messages so they will be purged if a consumer cannot process them on time.

Dependencies

Python >= 3.6 is required for channels_postgres

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_postgres-0.0.5.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

channels_postgres-0.0.5-py2.py3-none-any.whl (11.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file channels_postgres-0.0.5.tar.gz.

File metadata

  • Download URL: channels_postgres-0.0.5.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for channels_postgres-0.0.5.tar.gz
Algorithm Hash digest
SHA256 c2e913a0466da36e7ba599a0fa1878174c5e4ecc1f2ba3cc694f9828afdc9fc0
MD5 e7974b47f5d727ec06b72495090fe53b
BLAKE2b-256 1f05bac017a6941bbc0aa508e6d3cf6c67669e62176436985b076ff586ee5add

See more details on using hashes here.

File details

Details for the file channels_postgres-0.0.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for channels_postgres-0.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 855d7a41530d6cd5477d23c2b6662d81f345e32ec950a0616692c029c48ba936
MD5 90fc812ceddeec92b9d467c5f33f9048
BLAKE2b-256 e282f3e992c36290f2da7b39698092c48543e8205819b803ab4918875eb0c6de

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