https://github.com/evocount/drf-channels-oneway-ws
Simple one-way bindings for django-channels with some specific support for django-rest-framework serializers and websockets
https://github.com/evocount/drf-channels-oneway-ws
django django-channels django-rest-framework python websockets
Last synced: 7 days ago
JSON representation
Simple one-way bindings for django-channels with some specific support for django-rest-framework serializers and websockets
- Host: GitHub
- URL: https://github.com/evocount/drf-channels-oneway-ws
- Owner: evocount
- License: mit
- Created: 2019-03-21T17:23:48.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-27T18:38:47.000Z (about 5 years ago)
- Last Synced: 2025-05-18T08:17:50.757Z (about 1 month ago)
- Topics: django, django-channels, django-rest-framework, python, websockets
- Language: Python
- Size: 54.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://badge.fury.io/py/drf-channels-oneway-ws)
[](https://travis-ci.org/evocount/drf-channels-oneway-ws)
[](https://codecov.io/gh/evocount/drf-channels-oneway-ws)# 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
```python
from django.db import models
from rest_framework import serializers
from channels_oneway.bindings import Bindingclass 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 = Bird
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:```json
{
"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.
#### Registration
In order to let the bindings register their signals make sure they are imported at some point. In case you use a dedicated file, import it from [`AppConfig.ready`](https://docs.djangoproject.com/en/2.2/ref/applications/#django.apps.AppConfig.ready) just like your signals.### 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 [email protected]:evocount/drf-channels-oneway-ws.git`
* `cd drf-channels-oneway-ws`
* `pipenv install --dev`### Running tests
* `pipenv run pytest --cov`
## License
This project is licensed under the [MIT License](LICENSE.md).