Skip to content

Add Django Channels #49

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Allow splitting of groups for consumers
  • Loading branch information
tjquillan committed Nov 15, 2022
commit 4314bfa4fd4fa84747e72fed2f6297934f32dcd4
24 changes: 17 additions & 7 deletions question_queue/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ async def encode_json(cls, content):


class CoachLiveQueueConsumer(AsyncOrjsonWebsocketConsumer):
GROUP_NAME = "queue_group"
COACH_GROUP_NAME = "coach_queue_group"
STUDENT_GROUP_NAME = "student_queue_group"

async def connect(self):
stream = self.scope["url_route"]["kwargs"]["stream"]

await self.accept()
await self.channel_layer.group_add(self.GROUP_NAME, self.channel_name)
if stream == "coach":
await self.channel_layer.group_add(self.COACH_GROUP_NAME, self.channel_name)
elif stream == "student":
await self.channel_layer.group_add(
self.STUDENT_GROUP_NAME, self.channel_name
)

self.user = self.scope["user"]

Expand All @@ -37,7 +45,9 @@ async def receive_json(self, content):

await self.process_queue_action(question_id, action)

await self.channel_layer.group_send(self.GROUP_NAME, {"type": "html_message"})
await self.channel_layer.group_send(
self.COACH_GROUP_NAME, {"type": "html_message"}
)

@database_sync_to_async
def process_queue_action(self, question_id, action):
Expand All @@ -54,16 +64,16 @@ def process_queue_action(self, question_id, action):
def get_queue_questions(self):
table_data = []
for question in QueueQuestion.objects.filter(hidden=False):
attending=""
if (question.attending):
attending="table-primary"
attending = ""
if question.attending:
attending = "table-primary"
question_info = {
"id": str(question.id),
"name": question.asked_by.first_name,
"class": question.course.name,
"time": question.created_at,
"message": question.message,
"attending": attending
"attending": attending,
}

table_data.append(question_info)
Expand Down
2 changes: 1 addition & 1 deletion question_queue/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
from . import consumers

websocket_urlpatterns = [
re_path(r"ws/coach/$", consumers.CoachLiveQueueConsumer.as_asgi()),
re_path(r"ws/(?P<stream>\w+)/$", consumers.CoachLiveQueueConsumer.as_asgi()),
]