title: React + Flask-Socket.io: Socket disconnection event from client delayed when browser is closed?
body: <p><strong>My React component:</strong></p>
  <pre><code>import React, { useState, useEffect } from 'react';
  import {Redirect} from &quot;react-router-dom&quot;;
  import io from &quot;socket.io-client&quot;;
  
  function Game({userName, roomCode}) {
  
      const [users, setUsers] = useState([])
  
      useEffect(() =&gt; {
          const socket = io(&quot;http://127.0.0.1:5000&quot;)
          socket.emit('join', {userName, roomCode})
          socket.on('message', (msg) =&gt; console.log(msg))
          socket.on('updateUsers', (userlist) =&gt; {
              setUsers(Object.values(userlist))
          }) 
          return () =&gt; {
              socket.disconnect()
          }
          }, [])
  
      const userItems = users.map((element) =&gt;
          &lt;li&gt;{element.username}&lt;/li&gt;)
  
      return userName === '' ? &lt;Redirect to=&quot;/&quot;/&gt; :
          (
          &lt;div&gt;
              &lt;h1&gt;Game Page&lt;/h1&gt;
              &lt;h2&gt;Users:&lt;/h2&gt;
              &lt;ul&gt;{userItems}&lt;/ul&gt;
          &lt;/div&gt;
      );
  }
  
  export default Game;
  </code></pre>
  <p><strong>Flask:</strong></p>
  <pre><code>from flask import Flask, render_template, request
  from flask_socketio import SocketIO, send, join_room, leave_room, emit
  from users import UserDB
  
  app = Flask(__name__)
  app.config['SECRET_KEY'] = 'secret!'
  socketio = SocketIO(app, cors_allowed_origins=&quot;*&quot;)
  
  db = UserDB()
  
  @socketio.on('join')
  def on_join(data):
      print(&quot;Joining!&quot;)
      username = data['userName']
      room = data['roomCode']
      join_room(room)
      db.add_user(request.sid, username, room)
      room_list = db.get_room_users(room)
      emit('updateUsers', room_list, broadcast=True, room=room)
  
  
  @socketio.on('disconnect')
  def on_leave():
      user = db.delete_user(request.sid)
      room = user['room']
      room_list = db.get_room_users(room)
      emit('updateUsers', room_list, broadcast = True, room = room)
      print('disconnection!')
  
  
  if __name__ == '__main__':
      socketio.run(app, debug=True)
  </code></pre>
  <p>Now, if I press back on the browser (using React Router), the component unmounts and the socket disconnects instantly. However, if I close the tab, the socket only disconnects after around a minute. How do I make it so the socket disconnects as soon as the browser/tab closes (the way it works on Vanilla JS)?</p>