title: How to solve 401 Unauthorized error in Socket.IO Django framework?
body: <p>I am trying to get the Socket.IO work with my Django server. Here is my setup:</p>
  <p>Frontend js:</p>
  <pre><code>const socket = io.connect('127.0.0.1:8001');
  socket.on('connect', () =&gt; {
      console.log('socket id: %s\n', socket.id);
  });
  </code></pre>
  <p>Django server:</p>
  <pre><code>from django.core.wsgi import get_wsgi_application
  application = get_wsgi_application()
  
  sio = socketio.Server(async_mode='eventlet', cors_allowed_origins='*', logger=True, engineio_logger=True)
  
  @sio.event
  def connect(sid, environ, auth):
      print('connect ', sid, auth)
  
  static_files = {
      '/public': './static',
  }
  application = socketio.WSGIApp(sio, application, static_files=static_files)
  eventlet.wsgi.server(eventlet.listen(('', 8001)), application)
  </code></pre>
  <p>Dependencies</p>
  <pre><code>Django==2.2.11
  django-cors-headers==3.0.0
  eventlet==0.30.0
  gunicorn==19.7.1
  python-socketio==4.6.1
  ...
  </code></pre>
  <p>When I run the js, the server will return 401 unauthorized error before reaching the connect function.
  Frontend:</p>
  <pre><code>GET http://127.0.0.1:8001/socket.io/?EIO=3&amp;transport=polling&amp;t=NYKlRjO 401 (UNAUTHORIZED)
  </code></pre>
  <p>Django server log:</p>
  <pre><code>(11053) accepted ('127.0.0.1', 34906)
  127.0.0.1 - - [02/Apr/2021 15:39:31] &quot;GET /socket.io/?EIO=3&amp;transport=polling&amp;t=NYKlTB8 HTTP/1.1&quot; 401 253 0.002482
  </code></pre>
  <p>But the weird thing is if I commented out the connect event, everything like other events work just fine:</p>
  <pre><code># @sio.event
  # def connect(sid, environ, auth):
  #     print('connect ', sid, auth)
  </code></pre>
  <p>The Django server is running on the same port 8001. I don't think there is any authentication check on the connect event or on the socket. Anyone knows why if I setup the <code>connect</code> event and the socket suddenly stop working?</p>