Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket Server Breaks After Installing gevent-websocket #2122

Closed
mglst opened this issue Dec 11, 2024 · 4 comments
Closed

WebSocket Server Breaks After Installing gevent-websocket #2122

mglst opened this issue Dec 11, 2024 · 4 comments
Labels

Comments

@mglst
Copy link
Contributor

mglst commented Dec 11, 2024

I am encountering an issue where the WebSocket server functions correctly without gevent-websocket installed, but breaks once the library is added.

  • Without gevent-websocket: WebSocket connections to ws://127.0.0.1:5001/ws work as expected. I can successfully connect using:
    • My browser’s JS console.
    • An iOS app I'm developing.
      However, the following warning appears in the server console:
      WARNING in __init__: WebSocket transport not available. Install gevent-websocket for improved performance.
  • With gevent-websocket installed: The server accepts WebSocket connections but immediately closes them. I see a 101 Switching Protocols response in the network logs, followed by errors and a closed connection on the client side. The server does not report any anomalies

Here is the code in question. It's a minimal Flask app using both flask-sock and Flask-SocketIO, with gevent:

from gevent import monkey

monkey.patch_all(thread=True, time=True)

from flask import Flask
from flask_sock import Sock
from flask_socketio import SocketIO

app = Flask(__name__)
app.config["SECRET_KEY"] = "secret!"
socketio = SocketIO(app)
sock = Sock(app)


@sock.route("/ws")
def echo(ws):
    ws.send("Hello, World!")
    while True:
        data = ws.receive()
        ws.send(data)


if __name__ == "__main__":
    socketio.run(app, host="0.0.0.0", port=5001)

The above works with gevent-websocket if I remove socketio.

Here's the result of my pip freeze. These are all the latest versions I can install. I'm using python 3.12.8.

bidict==0.23.1
blinker==1.9.0
click==8.1.7
Flask==3.1.0
flask-sock==0.7.0
Flask-SocketIO==5.4.1
gevent==24.11.1
gevent-websocket==0.10.1
greenlet==3.1.1
h11==0.14.0
itsdangerous==2.2.0
Jinja2==3.1.4
MarkupSafe==3.0.2
python-engineio==4.10.1
python-socketio==5.11.4
setuptools==75.6.0
simple-websocket==1.1.0
Werkzeug==3.1.3
wsproto==1.2.0
zope.event==5.0
zope.interface==7.2
@miguelgrinberg miguelgrinberg transferred this issue from miguelgrinberg/flask-sock Dec 11, 2024
@miguelgrinberg
Copy link
Owner

gevent-websocket was released last in 2017. Why do you need it?

@mglst
Copy link
Contributor Author

mglst commented Dec 12, 2024

I need both WebSockets and Socketio, I don't specifically need gevent-websocket, I just happen to be using gevent. I'll just ignore the warning and move on. The warning tripped me up though, since I installed gevent-websocket long before running into this issue, and it took me a while to realise that it wasn't client side. Presumably the warning isn't coming from flask-sock?

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Dec 12, 2024

In the current version of Flask-SocketIO the gevent-websocket package is not required, in fact, it is best not to use it since it is unmaintained and has been for many years. I now realize that there is a warning in this package that is outdated. I'll remove it. WebSocket works fine without gevent-websocket in current versions, the simple-websocket package is used instead, so if you have that one installed you are good to go.

Also, you referenced Flask-Sock and initially wrote this issue on that project, but Flask-Sock has no connection to Flask-SocketIO. None at all, they are different solution, both use WebSocket, but that is as far as the connection goes.

@miguelgrinberg
Copy link
Owner

The warning has been removed, since it does not apply anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants