random one_to_one chat with flask

I am trying to build a flask app in which users can choose a pseudo and chat in one_to_one chat randomly with skip option , the problem here is that when user choose his username and redirect him to waiting page , it still stuck in there even when i open the website in an other browser (trying it on localhost and working on pychram)

(https://i.sstatic.net/MBnK1UGp.png)

import random
from flask import Flask, render_template, request, redirect, url_for, session, jsonify, send_from_directory
from datetime import datetime
import os

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['UPLOAD_FOLDER'] = 'uploads'
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)

messages = []
users = []
pairs = {}

@app.route('/')
def choose_username():
    return render_template('index.html')

@app.route('/set_username', methods=['POST'])
def set_username():
    username = request.form['name']
    session['username'] = username
    users.append(username)
    session['paired'] = False
    return redirect(url_for('chatroom'))

@app.route('/chatroom')
def chatroom():
    if 'username' not in session:
        return redirect(url_for('choose_username'))
    pair_user()
    return render_template('chat_room.html', messages=messages, username=session['username'], paired=session['paired'])

@app.route('/logout', methods=['POST'])
def logout():
    username = session.get('username')
    if username in users:
        users.remove(username)
    if username in pairs:
        partner = pairs.pop(username, None)
        if partner:
            pairs.pop(partner, None)
    session.pop('username', None)
    return redirect(url_for('choose_username'))

@app.route('/send_message', methods=['POST'])
def send_message():
    if 'username' not in session:
        return redirect(url_for('choose_username'))

    name = session['username']
    message_text = request.form['message']
    timestamp = datetime.now()
    file = request.files.get('file')
    file_path = None

    if file:
        filename = f"{timestamp.strftime('%Y%m%d%H%M%S')}_{file.filename}"
        file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(file_path)
        file_path = os.path.join('uploads', filename)

    new_message = {'name': name, 'message': message_text, 'timestamp': timestamp, 'file_path': file_path}
    messages.append(new_message)
    return redirect(url_for('chatroom'))

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

@app.route('/check_pairing')
def check_pairing():
    username = session.get('username')
    paired = session.get('paired', False)
    return jsonify({'paired': paired})

@app.route('/skip', methods=['POST'])
def skip():
    username = session.get('username')
    if username in pairs:
        partner = pairs.pop(username, None)
        if partner:
            pairs.pop(partner, None)
            clear_messages(username, partner)
    session['paired'] = False
    pair_user()  # Pair the skipping user with another random user
    return redirect(url_for('chatroom'))

def pair_user():
    global pairs
    if len(users) >= 2:
        available_users = [user for user in users if user not in pairs]
        if len(available_users) >= 2:
            user1 = random.choice(available_users)
            available_users.remove(user1)
            user2 = random.choice(available_users)
            pairs[user1] = user2
            pairs[user2] = user1
            session['paired'] = True

def clear_messages(user1, user2):
    global messages
    messages = [m for m in messages if m['name'] not in (user1, user2)]

if __name__ == '__main__':
    app.run(debug=True)

what i am expecting is when entering username if there is available user they will be paired directly and open chatroom for both of them and when one of them skip he will be paired with anyone else online
thank you

New contributor

Chahine Mansouri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật