Fly not letting Socketio work?

I have a flask project that utilizes Flask-Socketio, The project works just fine locally, but it breaks when I deploy it on, I know I am probably overlooking something here but I honestly spent hours on this and developed a bit of a tunnel vision, I hope someone can give me a helping hand here.


FROM python:3.11-bullseye


WORKDIR /flask-app

COPY ./requirements.txt .

RUN pip install --only-binary :all: greenlet
RUN pip install --only-binary :all: Flask-SQLAlchemy
RUN pip install -r requirements.txt

COPY . .


CMD ["python", ""]


# fly.toml app configuration file generated for appname on 2023-10-24T18:03:37+03:00
# See for information about how to use this file.

app = "appname"
primary_region = "cdg"


  internal_port = 5000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]

#!/bin/env python
from app import create_app, socketio

app = create_app(debug=True)

if __name__ == '__main__':, host='' , port=5000 )


        <title>Flask-SocketIO-Chat: {{ room }}</title>
        <script type="text/javascript" src="//"></script>
        <script type="text/javascript" src="//"></script>
        <script type="text/javascript" charset="utf-8">
            var socket;
                socket = io.connect('wss://' + document.domain + ':' + location.port + '/chat');
                socket.on('connect', function() {
                    socket.emit('joined', {});
                socket.on('status', function(data) {
                    $('#chat').val($('#chat').val() + '<' + data.msg + '>\n');
                socket.on('message', function(data) {
                    $('#chat').val($('#chat').val() + data.msg + '\n');
                $('#text').keypress(function(e) {
                    var code = e.keyCode || e.which;
                    if (code == 13) {
                        text = $('#text').val();
                        socket.emit('text', {msg: text});
            function leave_room() {
                socket.emit('left', {}, function() {

                    // go back to the login page
                    window.location.href = "{{ url_for('main.index') }}";
        <h1>Flask-SocketIO-Chat: {{ room }}</h1>
        <textarea id="chat" cols="80" rows="20"></textarea><br><br>
        <input id="text" size="80" placeholder="Enter your message here"><br><br>
        <a href="#" onclick="leave_room();">Leave this room</a>

Browser console:

[HTTP/2 400  69ms]


Firefox can’t establish a connection to the server at wss:// websocket.js:54:26
The connection to wss:// was interrupted while the page was loading. monitoring console:

2023-10-24T15:10:15.037 app[3287335a195618] cdg [info],, - - [24/Oct/2023 15:10:15] "GET / HTTP/1.1" 200 195 0.000131

2023-10-24T15:10:15.318 app[3287335a195618] cdg [info] (310) accepted ('', 46270)

2023-10-24T15:10:15.320 app[3287335a195618] cdg [info],, - - [24/Oct/2023 15:10:15] "POST / HTTP/1.1" 200 201 0.001092

2023-10-24T15:10:15.323 app[d891241fe51d18] cdg [info] (310) accepted ('', 44206)

2023-10-24T15:10:15.323 app[d891241fe51d18] cdg [info] Invalid session IXEClNUPlzR27_YCAAAA (further occurrences of this error will be logged with level INFO)

Hi - This is a complete shot in the dark, but: by default the deployment will create two machines, and the fact that in your log it complains about a mismatched session ID and there are two distinct logged session IDs for two Machines, makes me think the initial request that creates the session hits the first machine and the second request gets to the second machine, which knows nothing about the session and then barfs.

If so, you can probably test this theory by destroying one of your two machines (fly machine destroy) and seeing if things behave properly with only one machine handling all connections.

If that’s the case, then you need to look at how to deploy flask-socketio in a multi-worker, behind-a-balancing-proxy setup (which by the way is exactly what the deployment with two machines is) - this is well-explained in the flask-socketio page here.


  • Daniel

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.