Skip to main content

Easy to use class mixins for Django Rest Framework and Pusher to keep your API realtime.

Project description

PyPI version Build Status

DRF Model Pusher

Easy to use class mixins for Django Rest Framework and Pusher to keep your API realtime.

Installation

Download from PyPI:

pip install drf_model_pusher

Configuration

Settings Config

In your settings set your Pusher App Id and credentials, the cluster is optional

PUSHER_APP_ID=""  
PUSHER_KEY=""  
PUSHER_SECRET=""
PUSHER_CLUSTER=""  

Update Installed Apps

Add drf_model_pusher to your INSTALLED_APPS:

INSTALLED_APPS = [
    "...",
    "drf_model_pusher",
]

Implement Pusher Backends

Define some PusherBackends for your models and serializers in a pusher_backends.py file. The PusherBackend class just needs to define a serializer_class attribute which inherits from ModelSerializer.

# example/pusher_backends.py

from django.db.models import Model
from rest_framework.serializers import ModelSerializer
from drf_model_pusher.backends import PusherBackend, PrivatePusherBackend

class MyModel(Model):
    pass

class MyModelSerializer(ModelSerializer):
    class Meta:
        model = MyModel

class MyModelPrivateSerializer(ModelSerializer):
    class Meta:
        model = MyModel

class MyModelPusherBackend(PusherBackend):
    serializer_class = MyModelSerializer
    
class MyModelPrivatePusherBackend(PrivatePusherBackend):
    serializer_class = MyModelPrivateSerializer

Implement Views

Add the ModelPusherViewMixin mixin class to your views and define a get_pusher_channels method which should return a list of strings to use as channels.

# example/views.py

from rest_framework.viewsets import ModelViewSet
from drf_model_pusher.views import ModelPusherViewMixin
from example.serializers import MyModelSerializer

class MyModelViewSet(ModelPusherViewMixin, ModelViewSet):
    serializer_class = MyModelSerializer
    
    def get_pusher_channels(self):
        return ["<channel_id>"]

Ignoring the current connection

If you want to ignore the current connection when sending messages you should set a x-pusher-socket-id header on your requests. This may be useful if you're modifying resources and receiving the results in a response, you may not want the current connection to listen on these events to prevent duplicating content.

The PusherBackend.push_change method accepts an ignore boolean keyword argument which can toggle whether the pusher socket id is used, it defaults to True, so including the pusher socket id in the request will ignore the current connection for all pusher events sent by default.

Settings

  • DRF_MODEL_PUSHER_BACKENDS_FILE (default: pusher_backends.py) - The file in your applications to import PusherBackends.
  • DRF_MODEL_PUSHER_DISABLED (default: False) - Determines whether or not to trigger Pusher events.

Common Issues

Unregistered Backends

If you have followed the above steps correctly and your backends are not registering, your app config may not be running it's ready method. To force this, in your apps __init__.py add the line default_app_config = 'myapp.apps.MyAppConfig'

Pusher

Be aware of any pusher limits and consult their documentation at https://pusher.com/docs for some common questions.

Pusher has a 10kb default size limit on messages, this can be increased to 256kb by contacting support.

Extending PusherBackend

If you want to extend PusherBackend or PrivatePusherBackend rather than declaring a new concrete backend, you need to make sure the class is abstract. For example your new base class would be similar to this:

class MyPusherBackend(PusherBackend):
    class Meta:
        abstract = True

    # Override whatever methods here


class MyModelBackend(MyPusherBackend):
    class Meta:
        model = MyModel

Contributions

It's early days, but if you'd like to report any issues or work on an improvement then please check for any similar existing issues before you report them.

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_model_pusher-0.2.0.tar.gz (7.1 kB view details)

Uploaded Source

File details

Details for the file drf_model_pusher-0.2.0.tar.gz.

File metadata

  • Download URL: drf_model_pusher-0.2.0.tar.gz
  • Upload date:
  • Size: 7.1 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.23.4 CPython/3.6.2

File hashes

Hashes for drf_model_pusher-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9eb4142f4c68d090ee96e103ae45b856aac2116c0208b621f3fcabc473bc18b1
MD5 52898847a6d07c503444d6433d0ef796
BLAKE2b-256 4d146671e7e3dfedc2c378f36b63238508ed0ad5fe72f8aa6dbb9be8d83eeba0

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