0
0
Flaskframework~5 mins

Room-based messaging in Flask

Choose your learning style9 modes available
Introduction

Room-based messaging lets users chat in separate groups or rooms. It keeps conversations organized and private.

You want users to join different chat groups like sports or music rooms.
You need to separate messages so only people in the same room see them.
You want to build a live chat app with multiple topics.
You want to manage user access to specific chat areas.
You want to broadcast messages only to a selected group of users.
Syntax
Flask
from flask import Flask, render_template
from flask_socketio import SocketIO, join_room, leave_room, send

app = Flask(__name__)
socketio = SocketIO(app)

@socketio.on('join')
def on_join(data):
    username = data['username']
    room = data['room']
    join_room(room)
    send(f'{username} has entered the room.', to=room)

@socketio.on('leave')
def on_leave(data):
    username = data['username']
    room = data['room']
    leave_room(room)
    send(f'{username} has left the room.', to=room)

@socketio.on('message')
def handle_message(data):
    room = data['room']
    msg = data['msg']
    send(msg, to=room)

Use join_room(room) to add a user to a room.

Use leave_room(room) to remove a user from a room.

Examples
User joins the 'sports' room and a welcome message is sent only to that room.
Flask
join_room('sports')
send('Welcome to sports room!', to='sports')
User leaves the 'music' room and a message is sent to notify others in that room.
Flask
leave_room('music')
send('User left music room', to='music')
Messages are sent only to users in the specified room.
Flask
@socketio.on('message')
def handle_message(data):
    send(data['msg'], to=data['room'])
Sample Program

This Flask app uses Flask-SocketIO to let users join or leave chat rooms and send messages only to those rooms.

Flask
from flask import Flask, render_template
from flask_socketio import SocketIO, join_room, leave_room, send

app = Flask(__name__)
socketio = SocketIO(app)

@app.route('/')
def index():
    return 'Room-based messaging server running.'

@socketio.on('join')
def on_join(data):
    username = data['username']
    room = data['room']
    join_room(room)
    send(f'{username} has entered the room.', to=room)

@socketio.on('leave')
def on_leave(data):
    username = data['username']
    room = data['room']
    leave_room(room)
    send(f'{username} has left the room.', to=room)

@socketio.on('message')
def handle_message(data):
    room = data['room']
    msg = data['msg']
    send(msg, to=room)

if __name__ == '__main__':
    socketio.run(app, debug=True)
OutputSuccess
Important Notes

Make sure to install flask-socketio with pip install flask-socketio.

Rooms help keep chats organized and private.

Use browser DevTools Network tab to see WebSocket messages during testing.

Summary

Room-based messaging groups users for private chats.

Use join_room and leave_room to manage rooms.

Messages sent to a room reach only users in that room.