emit events from django views or django database changes via python-socketio
pip install django-axios
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
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
emit socketio event from api_view / http view
from django_socket.socket import http
@http(["GET"])
def webhook(request, socket):
# your code
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
# 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 for the current version of django-axios
is available from github README.