Skip to main content

Simple one-way bindings for django-channels with some specific support for django-rest-framework serializers and websockets. I.e. enrich your existing API with push notifications.

Project description

DRF channels one-way WS

Simple one-way bindings for django-channels with some specific support for django-rest-framework serializers and websockets. I.e. enrich your existing API with push notifications.

Installation

  • pipenv install drf-channels-oneway-ws or pip install drf-channels-oneway-ws

Usage

Bindings

from django.db import models
from rest_framework import serializers
from channels_oneway.bindings import Binding

class Family(models.Model):
    name = models.CharField(max_length=255)

class Bird(models.Model):
    name = models.CharField(max_length=255)
    family = models.ForeignKey('Family', models.CASCADE)


class BirdSerializer(serializers.ModelSerializer):
    class Meta:
        model = Customer
        fields = ('__all__')


class BirdBinding(Binding):
    model = Bird
    stream = 'birds'
    serializer = BirdSerializer

    @classmethod
    def group_names(cls, instance):
        return [instance.family.name]

class FamilyBinding(Binding):
    """
    example of a binding not using a drf serializer
    """
    model = Family
    stream = 'bird-families'

    @classmethod
    def group_names(cls, instance):
        return [instance.name]

    def serialize_data(self, instance):
        return {'id': instance.id, 'name': instance.name}

Now you make sure you have a WebsocketConsumer, which does something like self.channel_layer.group_add('thrushes', self.channel_name) in its connect coroutine. Family.objects.create(name='thrushes') will then cause the following to be sent over the associated websocket:

{
    "stream": "bird-families",
    "payload": {
        "action": "create",
        "data": {"id": 1, "name": "thrushes"},
        "model": "your_app.family",
        "pk": 1
    }
}

Upon modification ("action": "update") or deletion ("action": "delete") you will receive messages with an equal structure.

Helpers

In order to send a ws message from outside a binding, but using the same format (stream + payload) (and also the drf json encoder) use the async channels_oneway.utils.groupSend(group, stream, payload) or its sync equivalent groupSendSync.

Contributing

Installation

  • git clone git@github.com:evocount/drf-channels-oneway-ws.git
  • cd python-set-ings
  • pipenv install --dev

Running tests

  • pipenv run pytest --cov

License

This project is licensed under the MIT License.

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

drf-channels-oneway-ws-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

drf_channels_oneway_ws-0.1.0-py2.py3-none-any.whl (6.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file drf-channels-oneway-ws-0.1.0.tar.gz.

File metadata

  • Download URL: drf-channels-oneway-ws-0.1.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.2

File hashes

Hashes for drf-channels-oneway-ws-0.1.0.tar.gz
Algorithm Hash digest
SHA256 574b113875cc961f7a402e49c1ac6d7e507841987ab2ddc1831d4d0b5d839bc3
MD5 5c619599260dc8599a4cacb5f604d738
BLAKE2b-256 6f50e167fadc19fb393546c628eaf3fe43e6a173d60bee69d04cd11dc2031a09

See more details on using hashes here.

File details

Details for the file drf_channels_oneway_ws-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: drf_channels_oneway_ws-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.2

File hashes

Hashes for drf_channels_oneway_ws-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 977fd607eb29565473b6dcce11a82c2acd3ba899c9dcf5f6eb3983a00bfa8faa
MD5 3c538187ade7080897497c113c092b91
BLAKE2b-256 fd69395b33b5de3dc14b67263e55280c097b08fcc6982b1679d6d8bb9fa1f741

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