Bird
0
0

What is wrong with this Flask-SocketIO server code that tries to send real-time updates?

medium📝 Debug Q14 of 15
Flask - WebSocket and Real-Time
What is wrong with this Flask-SocketIO server code that tries to send real-time updates?
from flask_socketio import SocketIO, emit

socketio = SocketIO(app)

@socketio.on('connect')
def on_connect():
    emit('update', {'data': 'Welcome!'})

if __name__ == '__main__':
    app.run()
AThe event name 'update' is reserved and cannot be used.
BThe emit function cannot be used inside the connect event.
CThe SocketIO instance is missing the async_mode parameter.
DThe server should use socketio.run(app) instead of app.run() to support WebSocket.
Step-by-Step Solution
Solution:
  1. Step 1: Check how Flask-SocketIO server runs

    Flask-SocketIO requires socketio.run(app) to enable WebSocket support properly.
  2. Step 2: Identify the mistake in app.run()

    Using app.run() starts Flask's default server, which does not support WebSocket needed for real-time.
  3. Final Answer:

    The server should use socketio.run(app) instead of app.run() to support WebSocket. -> Option D
  4. Quick Check:

    Use socketio.run(app) for real-time [OK]
Quick Trick: Use socketio.run(app), not app.run(), for real-time [OK]
Common Mistakes:
MISTAKES
  • Thinking emit can't be used on connect event
  • Believing event names like 'update' are reserved
  • Assuming async_mode is always required

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes