from flask import Flask from flask_socketio import SocketIO, join_room, emit # Initialize the Flask app and SocketIO instance app = Flask(__name__) socketio = SocketIO(app) # Handle the connection event @socketio.on('connect') def handle_connect(): print("User connected") # Handle room joining @socketio.on('joinRoom') def handle_join_room(room_id): join_room(room_id) print(f"User joined room: {room_id}") # Handle receiving and broadcasting chat messages @socketio.on('chatMessage') def handle_chat_message(msg, room_id): print(f"Message received: {msg}") # Broadcast the message to everyone in the room emit('chatMessage', msg, room=room_id) # Handle user disconnection @socketio.on('disconnect') def handle_disconnect(): print("User disconnected") # Start the server when running this script if __name__ == '__main__': # Run the server on the Hugging Face Space environment socketio.run(app, host='0.0.0.0', port=5000)