Skip to content

emit events from django views or django database changes via python-socketio

License

Notifications You must be signed in to change notification settings

oasisMystre/django-axios

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Axios

emit events from django views or django database changes via python-socketio

pip install django-axios

Usage

in your wsgi.py or asgi.py file add this at the bottom of your file

...

from socketio import WSGIApp

from django_axios.socket import sio

application = WSGIApp(sio, application)

Note: make sure to add it to the very bottom of the file to prevent unexpected behavior

Signals

listen to database changes using the event decorator

from django.db.models.signal import post_save

from django_socket.socket import event

@event(post_save, sender=Model, serializer)
def on_model_save(instance, serializer, socket, created):
    # your code

Requests

emit socketio event from api_view / http view

from django_socket.socket import http

@http(["GET"])
def webhook(request, socket):
    # your code

Decorators

use rest_framework middleware decorators with @http

from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import permission_classes

@http(['GET'])
@permission_classes([IsAuthenticated])
def webhook(request, sio):
    # your code

incase you have custom socketio instance initiated

# demo/instance.py

from socketio import Server 

# variable name must be sio or an exception will be raised
sio = Server(async_mode="threading")

in your settings.py

DJANGO_AXIOS = {
    'socket': 'demo.instance'
}

Documentation

Documentation for the current version of django-axios is available from github README.

"Buy Me A Coffee"

About

emit events from django views or django database changes via python-socketio

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages