code comments
modified: app.py
This commit is contained in:
parent
5abd5fa516
commit
0b0ef8c792
1 changed files with 7 additions and 0 deletions
7
app.py
7
app.py
|
|
@ -62,6 +62,11 @@ def on_join(data):
|
|||
# Fetch previous messages from the database
|
||||
previous_messages = Message.query.filter_by(room=room).all()
|
||||
|
||||
|
||||
# Send the history of messages only to the newly connected client.
|
||||
# The reason for using `request.sid` here is to target the specific session (or client) that
|
||||
# just connected, so only they receive the backlog of messages, rather than broadcasting
|
||||
# this information to all clients in the room.
|
||||
for message in previous_messages:
|
||||
emit(
|
||||
"previous_messages",
|
||||
|
|
@ -73,6 +78,8 @@ def on_join(data):
|
|||
room=request.sid,
|
||||
)
|
||||
|
||||
# Broadcast to all clients in the room that a new user has joined.
|
||||
# Here, `room=room` ensures the message is sent to everyone in that specific room.
|
||||
emit(
|
||||
"message",
|
||||
{"id": None, "content": f"{data['username']} has joined the room."},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue