diff --git a/docker-compose.yml b/docker-compose.yml index d6c1fda..cf1544b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,7 +22,7 @@ services: - .env environment: - FLASK_APP=blueprints.__init__ - - FLASK_DEBUG=True + - FLASK_ENV=production frontend: build: diff --git a/frontend/src/components/Stream/StreamerRoute.tsx b/frontend/src/components/Stream/StreamerRoute.tsx index 701e16c..87f254e 100644 --- a/frontend/src/components/Stream/StreamerRoute.tsx +++ b/frontend/src/components/Stream/StreamerRoute.tsx @@ -26,7 +26,7 @@ const StreamerRoute: React.FC = () => { checkStreamStatus(); // Poll for live status changes - const interval = setInterval(checkStreamStatus, 1000); // Check every 90 seconds + const interval = setInterval(checkStreamStatus, 30000); // Check every 90 seconds return () => clearInterval(interval); }, [streamerName]); diff --git a/web_server/blueprints/chat.py b/web_server/blueprints/chat.py index 0b8003a..a090e7a 100644 --- a/web_server/blueprints/chat.py +++ b/web_server/blueprints/chat.py @@ -2,20 +2,19 @@ from flask import Blueprint, jsonify, session from database.database import Database from flask_socketio import SocketIO, emit, join_room, leave_room from datetime import datetime +from flask_socketio import SocketIO chat_bp = Blueprint("chat", __name__) -socketio = SocketIO() +socketio = SocketIO(cors_allowed_origins="*") # <---------------------- ROUTES NEEDS TO BE CHANGED TO VIDEO OR DELETED AS DEEMED APPROPRIATE ----------------------> -# TODO: Add a route that deletes all chat logs when the stream is finished - @socketio.on("connect") def handle_connection() -> None: """ Accept the connection from the frontend. """ - print("Client Connected") # Confirmation connect has been made + print("Client Connected") # Confirmation connect has been made @socketio.on("join") @@ -62,14 +61,13 @@ def get_past_chat(stream_id: int): FROM chat WHERE stream_id = ? ORDER BY time_sent DESC - LIMIT 1 + LIMIT 50 ) ORDER BY time_sent ASC;""", (stream_id,)).fetchall() db.close_connection() # Create JSON output of chat_history to pass through NGINX proxy - chat_history = [{"chatter_id": chat[0], "message": chat[1], - "time_sent": chat[2]} for chat in all_chats] + chat_history = [{"chatter_id": chat[0], "message": chat[1], "time_sent": chat[2]} for chat in all_chats] # Pass the chat history to the proxy return jsonify({"chat_history": chat_history}), 200 @@ -91,7 +89,19 @@ def send_chat(data) -> None: emit("error", {"error": "Unable to send a chat"}, broadcast=False) return - # Save chat information to database so other users can see + # Send the chat message to the client so it can be displayed + emit("new_message", { + "chatter_id": chatter_id, + "message": message, + "time_sent": datetime.now().strftime("%Y-%m-%d %H:%M:%S") + }, room=stream_id) + + # Asynchronously save the chat + save_chat(chatter_id, stream_id, message) + + +def save_chat(chatter_id, stream_id, message): + """Save the chat to the database""" db = Database() cursor = db.create_connection() cursor.execute(""" @@ -99,10 +109,3 @@ def send_chat(data) -> None: VALUES (?, ?, ?);""", (chatter_id, stream_id, message)) db.commit_data() db.close_connection() - - # Send the chat message to the client so it can be displayed - emit("new_message", { - "chatter_id": chatter_id, - "message": message, - "time_sent": datetime.now().strftime("%Y-%m-%d %H:%M:%S") - }, room=stream_id)