from channels_presence.models import Room
Manager:
Instance properties:
Instance methods:
from channels_presence.models import Presence
Manager:
Instance properties:
from chanels_presence.decorators import touch_presence
Decorator for use on websocket.receive handlers which updates the last_seen timestamp on any Presence instances associated with the client. If the message being sent is the literal JSON-encoded "heartbeat", message processing stops and the decorator does not call the decorated function.
Example:
@touch_presence
def ws_receive(message, *args, **kwargs):
# ... process any received message except "heartbeat" ...
pass
from chanels_presence.decorators import remove_presence
Decorator for use on websocket.disconnect handlers which removes any Presence instances associated with the client.
Example:
@remove_presence
def ws_disconnect(message):
pass
from channels_presence.signals import presence_changed
This is sent on any addition or removal of a Presence from a Room. Use it to track when users come and go.
Arguments sent with this signal:
Example:
import json
from django.dispatch import receiver
from channels import Group
from channels_presence.signals import presence_changed
def broadcast_to_room(room, message):
Group(room.channel_name).send({
'text': json.dumps(message)
})
@receiver(presence_changed)
def handle_presence_changed(sender, room, added, removed, bulk_change):
if added:
broadcast_to_room({'added': added.channel_name})
if removed:
broadcast_to_room({'removed': removed.channel_name})
if bulk_change:
broadcast_to_room({'presence': [p.channel_name for p in room.presence_set.all()]})
from channels_presence.channels import ws_disconnect
This is a convenience handler which can be installed to always clean up presence on disconnect. Use it if you don’t have any other particular logic that needs to happen on disconnect.
Example in channels routing:
from channels.routing import route
channel_routing = [
route("websocket.disconnect", "channels_presence.channels.ws_disconnect"),
]
The implementation is simply:
# channels_presence/channels.py
from channels_presence.decorators import remove_presence
@remove_presence
def ws_disconnect(message, **kwargs):
pass