Flask-SocketIO: Socket.io not working on my machine

I’m new in developing and Flask I had 2 projects during my course , the error occurs in the second one only and only when socket.io exist , it’s an empty project but Flask behaves very weird.

for example, every time I run Flask without socket IO it reads something like this

D:\WORK\study sc5\project1\project1>flask run
Serving Flask app "hello"
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

in project 2 we had to work with socket.io and I know it’s the reason because I deleted its line and the weird behavior were gone , also no errors when I ran the socket IO application I get

D:\WORK\study sc5\project2\project2>flask run
 * Serving Flask-SocketIO app "application"

that is it after a while I get

[WinError 10053] An established connection was aborted by the software in your host machine
Sun Oct  7 23:47:57 2018 {'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '56449', 'HTTP_HOST': '127.0.0.1:5000', (hidden keys: 23)} failed with ConnectionAbortedError

with that said here is my simple python code


import os, passlib ,requests ,time

from flask import Flask, session , render_template , request,redirect,url_for
from flask_socketio import SocketIO, emit ,join_room, leave_room , Namespace
from datetime import date , datetime
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from passlib.hash import sha256_crypt


engine = create_engine("postgres:**************************",echo=True)
db = scoped_session(sessionmaker(bind=engine))



app = Flask(__name__)
app.config["SECRET_KEY"] = 'secrettt'
#sess.init_app(app)
socketio = SocketIO(app)

class NRoom:
    def __init__(self,Rowner,Rname,Rpw):
        self.Rowner = Rowner
        self.Rname = Rname
        self.Rpw = Rpw


class MyCustomNamespace(Namespace):
def on_connect(self):
    pass

def on_disconnect(self):
    pass

def on_my_event(self, data):
    emit('my_response', data)

socketio.on_namespace(MyCustomNamespace('/test'))


@socketio.on('message')
def handle(msg):
    print(f'\n\n\n Message:{msg}\n\n\n' )
    send(msg, broadcast=True)


@app.route("/")
def index():
    return render_template("index.html")

@app.route("/guest" , methods = ["GET" , "POST"])
def guest():

    if request.method == "POST":
    nickname = request.form.get("guestname")
    session["user"] = nickname
    print(f"\n\n\n {nickname} \n\n\n\n ")

    return redirect(url_for('guest',guestname=nickname))
else:


    return render_template("guest.html")

index html file

<!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width">
            <title>welcome bk</title>
        </head>
        <body>
            <script type="text/javascript" src="{{url_for('static', filename='javascript.js')}}"></script>
            <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
            <script type="text/javascript" charset="utf-8">

            var socket = io.connect('http://' + document.domain + ':' + location.port);
            socket.on('connect', function() {
            socket.emit('my event', {data: 'I\'m connected!'});
            });
        </script>


        <script>

                document.addEventListener('DOMCOntentLoaded',function(){

                    var socket = io.connect('http://127.0.0.1:5000');

                    socket.on('connect',function() {
                        socket.send("test socketio have passed ! ");

                    });
                });

        </script>
        <form action="{{ url_for('login') }}">
             <input type="submit" value="login" />
        </form>

        <form action="{{ url_for('regist') }}">
             <input type="submit" value="regist" />
        </form>


        <form action="{{ url_for('guest') }}">
             <input type="submit" value="login as guest" />
        </form>
    </body>
</html>

tested it with chrome and Firefox

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 43 (15 by maintainers)

Most upvoted comments

iam so tired of trying just to get it to work :S

i have installed Cygwin and thatis what happen when i run project 1 which have no Socket io

D:\Work\study\project1>flask run

  • Serving Flask-SocketIO app “application.py”
  • Forcing debug mode off
  • Serving Flask app “application.py”
  • Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead.
  • Debug mode: off
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

when i ran project 2 now it shows an error ( which socketIO ):

D:\Work\study\project2org\project2>flask run

  • Serving Flask-SocketIO app “application.py”
  • Forcing debug mode off Traceback (most recent call last): File “c:\users\mena\appdata\local\programs\python\python37-32\lib\runpy.py”, l ine 193, in run_module_as_main “main”, mod_spec) File “c:\users\mena\appdata\local\programs\python\python37-32\lib\runpy.py”, l ine 85, in run_code exec(code, run_globals) File "C:\Users\Mena\AppData\Local\Programs\Python\Python37-32\Scripts\flask.ex e_main.py", line 9, in <module> File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\flask\cli.py”, line 894, in main cli.main(args=args, prog_name=name) File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\flask\cli.py”, line 557, in main return super(FlaskGroup, self).main(*args, **kwargs) File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\click\core.py”, line 717, in main rv = self.invoke(ctx) File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\click\core.py”, line 1137, in invoke return process_result(sub_ctx.command.invoke(sub_ctx)) File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\click\core.py”, line 956, in invoke return ctx.invoke(self.callback, **ctx.params) File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\click\core.py”, line 555, in invoke return callback(*args, **kwargs) File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\click\decorators.py”, line 64, in new_func return ctx.invoke(f, obj, *args, **kwargs) File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\click\core.py”, line 555, in invoke return callback(*args, **kwargs) File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\flask_socketio\cli.py”, line 68, in run run_server() File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\flask_socketio\cli.py”, line 62, in run_server use_reloader=False, log_output=debugger) File "c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\flask_socketio_init.py", line 526, in run run_server() File "c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\flask_socketio_init.py", line 505, in run_server eventlet_socket = eventlet.listen(addresses[0][4], addresses[0][0]) File “c:\users\mena\appdata\local\programs\python\python37-32\lib\site-package s\eventlet\convenience.py”, line 78, in listen sock.bind(addr) OSError: [WinError 10048] Only one usage of each socket address (protocol/networ k address/port) is normally permitted

then maybe logging for python or flask can provide us with a clue of where the smoke is coming from ?

it would help if there is a way to read loggin from my machine about socket io

i’m sorry as i’ve mentioned earlier i’m googleing and trying different thing just to help us figure it out so i tried to remove socket io and eventlet and reinstall them but i ran into a problem i thought it would share it with you maybe it happen because it might be a clue that something is conflicting with socketIo but i managed to resolve them , thanks to u and stackoverflow

now i tried to run the code after the fresh installation i got the same result

D:\Work\study\project2org\project2>flask run
 * Serving Flask-SocketIO app "application.py"
 * Forcing debug mode off

dude literally that is all i have in the code which everyone in my course have it as well but no one with this problem :S

project2.zip

when it run with cgywin >>>>

Mena@Mena-PC /cygdrive/d/work/study/project2org/project2
$ export FLASK_APP="application.py"

Mena@Mena-PC /cygdrive/d/work/study/project2org/project2
$ flask run

when i try it with CMD >>>>>

D:\WORK\study sc5\project2org\project2>flask run

Serving Flask-SocketIO app "application
Forcing debug mode off

it say server is running on 127.0.0.1:5000 if the code doesnt have socket io in it , if it does it only says what i have mentioned above and yes when i visit 127.0.0.1:5000 i see it working but abslout silence if i tried to send anything from socket io

i had a different version as well , no errors at all , it prints anything related to python but none from socket io

app-test.zip