Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create consumer hooks #3

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@
from channels_yroom.consumer import YroomConsumer

class TextCollaborationConsumer(YroomConsumer):
room_name_prefix: str = "yroom"
room_url_kwargs: str = "room_name"

# or

def get_room_group_name(self) -> str:
"""
Determine a unique name for this room, e.g. based on URL
"""
room_name = self.scope["url_route"]["kwargs"]["room_name"]
return "textcollab_%s" % room_name


# use hooks or override the default

# hook:

async def pre_connect(self) -> None:
user = self.scope["user"]
if not user.is_staff:
await self.close()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a problem: how to return in the parent function to not execute the next code instructions?


# override the default
async def connect(self) -> None:
"""
Optional: perform some sort of authentication
Expand Down Expand Up @@ -114,3 +130,31 @@ sequenceDiagram
WebsocketConsumerB->>Bob: forward update
end
```


### Consumer Hooks

These hooks are called at the times described (pre and post) when the consumer events of the channel library are executed.

```py
class TextCollaborationConsumer(YroomConsumer):
async def pre_connect(self):
pass

async def post_connect(self):
pass

async def pre_disconnect(self):
pass

async def post_disconnect(self):
pass

async def pre_receive(self, text_data: Optional[str] = None, bytes_data: Optional[bytes] = None
) -> None:
pass

async def post_receive(self, text_data: Optional[str] = None, bytes_data: Optional[bytes] = None
) -> None:
pass
```
38 changes: 36 additions & 2 deletions channels_yroom/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,45 @@
logger = logging.getLogger(__name__)


class YroomConsumer(AsyncWebsocketConsumer):
class YRoomConsumerHooks:
async def pre_connect(self):
pass

async def post_connect(self):
pass

async def pre_disconnect(self):
pass

async def post_disconnect(self):
pass

async def pre_receive(self, text_data: Optional[str] = None, bytes_data: Optional[bytes] = None
) -> None:
pass

async def post_receive(self, text_data: Optional[str] = None, bytes_data: Optional[bytes] = None
) -> None:
pass

class YroomConsumer(AsyncWebsocketConsumer, YRoomConsumerHooks):
room_name_prefix: str = "yroom"
room_url_kwargs: str | None = None


def get_room_group_name(self) -> str:
return "yroom_default"
room_name = "default"
if self.room_url_kwargs is not None:
room_name = self.scope["url_route"]["kwargs"][self.room_url_kwargs]
return f"{self.room_name_prefix}_{room_name}"

def get_connection_id(self) -> int:
return random.getrandbits(64)

async def connect(self) -> None:
await self.pre_connect() # Hook
await self.join_room()
await self.post_connect() # Hook

async def join_room(self) -> None:
# Join room group
Expand All @@ -45,7 +75,9 @@ async def join_room(self) -> None:
)

async def disconnect(self, close_code) -> None:
await self.pre_disconnect() # Hook
await self.leave_room()
await self.post_disconnect() # Hook

async def leave_room(self) -> None:
# Leave room group
Expand All @@ -66,9 +98,11 @@ async def leave_room(self) -> None:
async def receive(
self, text_data: Optional[str] = None, bytes_data: Optional[bytes] = None
) -> None:
await self.pre_receive(text_data, bytes_data) # Hook
# Receive message from WebSocket
if bytes_data:
await self.handle_room_message(bytes_data)
await self.post_receive(text_data, bytes_data) # Hook

async def handle_room_message(self, bytes_data: bytes) -> None:
await self.channel_layer.send(
Expand Down