title: Flask-Socketio:Error during WebSocket handshake: Unexpected response code: 503 & WebSocket is closed before the connection is established
body: <p>So, I am trying to deploy my chat app to Heroku I saw some answers but none of them are with flask-socketio and they are with solutions that you can't do in flask-socketio. I followed the steps in <a href="https://flask-socketio.readthedocs.io/en/latest/#gunicorn-web-server" rel="nofollow noreferrer">here</a>. In addition you can there is an error in the logs</p>
  <pre><code>2020-07-09T14:55:03.811700+00:00 app[web.1]: Traceback (most recent call last):
  2020-07-09T14:55:03.811746+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.8/site-packages/eventlet/hubs/hub.py&quot;, line 461, in fire_timers
  2020-07-09T14:55:03.811747+00:00 app[web.1]: timer()
  2020-07-09T14:55:03.811751+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.8/site-packages/eventlet/hubs/timer.py&quot;, line 59, in __call__
  2020-07-09T14:55:03.811752+00:00 app[web.1]: cb(*args, **kw)
  2020-07-09T14:55:03.811754+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.8/site-packages/eventlet/semaphore.py&quot;, line 147, in _do_acquire
  2020-07-09T14:55:03.811754+00:00 app[web.1]: waiter.switch()
  2020-07-09T14:55:03.811783+00:00 app[web.1]: File &quot;src/gevent/greenlet.py&quot;, line 858, in gevent._gevent_cgreenlet.Greenlet.run
  2020-07-09T14:55:03.811811+00:00 app[web.1]: File &quot;src/gevent/greenlet.py&quot;, line 829, in gevent._gevent_cgreenlet.Greenlet._Greenlet__report_result
  2020-07-09T14:55:03.811814+00:00 app[web.1]: File &quot;src/gevent/_gevent_cgreenlet.pxd&quot;, line 45, in gevent._gevent_cgreenlet.get_my_hub
  2020-07-09T14:55:03.811863+00:00 app[web.1]: TypeError: Cannot convert greenlet.greenlet to gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop
  2020-07-09T14:55:03.812531+00:00 heroku[router]: at=info method=POST path=&quot;/socket.io/?EIO=3&amp;transport=polling&amp;t=NCq5C0_&amp;sid=59a6f168828134cc39e7314cf4639909f&quot; host=chat-py.herokuapp.com request_id=9c21cf77-3a08-44b9-917b-da87c2dad3fc fwd=&quot;176.229.186.130&quot; dyno=web.1 connect=2ms service=7ms status=200 bytes=202 protocol=http
  2020-07-09T14:55:33.809904+00:00 heroku[router]: at=error code=H12 desc=&quot;Request timeout&quot; method=GET path=&quot;/socket.io/?EIO=3&amp;transport=polling&amp;t=NCq5C13&amp;sid=59a63f16888134cc39e7314cf4639909f&quot; host=chat-py.herokuapp.com request_id=5ca46303-e131-4a64-844a-0070916051dc fwd=&quot;176.229.186.130&quot; dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=http
  2020-07-09T14:55:33.920874+00:00 heroku[router]: at=error code=H12 desc=&quot;Request timeout&quot; method=GET path=&quot;/socket.io/?EIO=3&amp;transport=websocket&amp;sid=59a6f16888234cc39e7314cf4639909f&quot; host=chat-py.herokuapp.com request_id=87ec71b4-c64a-4e3a-bf31-c6fda30eda3f2 fwd=&quot;176.229.186.130&quot; dyno=web.1 connect=2ms service=30000ms status=503 bytes=0 protocol=http
  </code></pre>
  <p>I can see the site up and running but for some reason there is a problem connecting to the socket-io in the network tab (you can't send messages and it worked in localhost).</p>
  <p>network tab
  <a href="https://i.stack.imgur.com/3qg6U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3qg6U.png" alt="network tab" /></a></p>
  <blockquote>
  <p>Pipfile:</p>
  </blockquote>
  <pre><code>flask = &quot;*&quot;
  flask-sqlalchemy = &quot;*&quot;
  flask-socketio = &quot;*&quot;
  flask-login = &quot;*&quot;
  sqlalchemy = &quot;*&quot;
  gunicorn = &quot;*&quot;
  psycopg2 = &quot;*&quot;
  eventlet = &quot;*&quot;
  gevent-websocket = &quot;*&quot;
  </code></pre>
  <blockquote>
  <p>Procfile:</p>
  </blockquote>
  <pre><code>web: gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 app:app
  </code></pre>
  <p>(the server name is app)</p>
  <blockquote>
  <p>connecting to socketio:</p>
  </blockquote>
  <pre><code>&lt;script src=&quot;//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js&quot; integrity=&quot;hidden&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  </code></pre>
  <blockquote>
  <p>start of the frontend code (JS):</p>
  </blockquote>
  <pre><code>$(document).ready(function(){
        var socket = io(&quot;http://chat-py.herokuapp.com:&quot; + location.port);
  .....
  </code></pre>
  <blockquote>
  <p>Heroku logs</p>
  </blockquote>
  <pre><code>remote:  !     Python has released a security update! Please consider upgrading to python-3.8.3
  remote:        Learn More: https://devcenter.heroku.com/articles/python-runtimes
  remote: -----&gt; No change in requirements detected, installing from cache
  remote: -----&gt; Installing dependencies with Pipenv 2018.5.18…
  remote:        Installing dependencies from Pipfile.lock (570f04)…
  remote:        Ignoring cffi: markers 'platform_python_implementation == &quot;CPython&quot; and sys_platform == &quot;win32&quot;' don't match your environment
  remote:        You are using pip version 9.0.2, however version 20.1.1 is available.
  remote:        You should consider upgrading via the 'pip install --upgrade pip' command.
  </code></pre>
  <blockquote>
  <p>UPDATED network tab:</p>
  </blockquote>
  <p>after using the cors_allowed_origins option configuration I was left with a single error (I am pretty sure that the cause of the error is in the logs):
  <a href="https://i.stack.imgur.com/cQU9B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cQU9B.png" alt="enter image description here" /></a></p>