Skip to content

Commit

Permalink
update settings, consumers, py dep, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 13, 2024
1 parent bc7863e commit 0546732
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
13 changes: 13 additions & 0 deletions chat-room/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ python manage.py migrate
# run app
# (make sure at same level with manage.py)
python3 manage.py runserver


# start redis
brew services start redis

# check
redis-cli ping
```

```bash
Expand Down Expand Up @@ -42,6 +49,12 @@ sqlite3 db.sqlite3
- http://127.0.0.1:8000/admin/
- user, pwd : `admin`


## Concept

- channel : A channel is a mailbox where messages can be sent to. Each channel has a name. Anyone who has the name of a channel can send a message to the channel.
- group : A group is a group of related channels. A group has a name. Anyone who has the name of a group can add/remove a channel to the group by name and send a message to all channels in the group. It is not possible to enumerate what channels are in a particular group.

## Ref

- https://github.com/django/channels
Expand Down
28 changes: 24 additions & 4 deletions chat-room/mysite/chat/consumers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import json

from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer


# https://channels.readthedocs.io/en/latest/tutorial/part_2.html
"""
This is a synchronous WebSocket consumer
Expand All @@ -11,16 +10,37 @@
For now it does not broadcast messages to other clients in the same room.
"""


class ChatConsumer(WebsocketConsumer):
def connect(self):
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
self.room_group_name = f"chat_{self.room_name}"

# Join room group
async_to_sync(self.channel_layer.group_add)(
self.room_group_name, self.channel_name
)

self.accept()

def disconnect(self, close_code):
pass
# Leave room group
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name, self.channel_name
)

# Receive message from WebSocket
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json["message"]

# Send message to room group
async_to_sync(self.channel_layer.group_send)(
self.room_group_name, {"type": "chat.message", "message": message}
)

# Receive message from room group
def chat_message(self, event):
message = event["message"]

# Send message to WebSocket
self.send(text_data=json.dumps({"message": message}))
14 changes: 13 additions & 1 deletion chat-room/mysite/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@

from pathlib import Path

# https://channels.readthedocs.io/en/latest/tutorial/part_2.html

ASGI_APPLICATION = "mysite.asgi.application"

CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
},
},
}

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand All @@ -27,7 +40,6 @@

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
Expand Down
3 changes: 2 additions & 1 deletion chat-room/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
django
channels
daphne
daphne
channels_redis

0 comments on commit 0546732

Please sign in to comment.