question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

socketio-client won't connect to wss flask-socketio server

See original GitHub issue

I get the following error when trying to connect to a SSL encrypted Flask-socketio server using the websocket transport.

Attempting WebSocket connection to wss://localhost/socket.io/?transport=websocket&EIO=3
Traceback (most recent call last):
  File "client.py", line 6, in <module>
    sio.connect('https://localhost', transports=['websocket'])
  File "/Library/Python/3.7/site-packages/socketio/client.py", line 279, in connect
    six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
  File "<string>", line 3, in raise_from
socketio.exceptions.ConnectionError: Connection error

Is there something more I need to configure for WSS to work? Unencrypted web sockets seem to work just fine.

Server.py

from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, logger=True)

socketio.run(app, host='0.0.0.0', port=443, debug=True, keyfile='snakeoil.key', certfile='snakeoil.pem')

Client.py

import socketio

sio = socketio.Client(ssl_verify=False, logger=True, engineio_logger=True)
sio.connect('https://localhost', transports=['websocket'])
sio.disconnect()

Flask==1.1.1 Flask-SocketIO==4.3.0 python-engineio==3.12.1 python-socketio==4.5.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Professor-plumcommented, May 8, 2020

enabling engineio_logger shed enough light on the issue.

(84096) wsgi starting up on https://0.0.0.0:8080
(84096) accepted ('127.0.0.1', 65163)
http://localhost:8080 is not an accepted origin.
127.0.0.1 - - [08/May/2020 14:23:42] "GET /socket.io/?transport=websocket&EIO=3&t=1588969422.69502 HTTP/1.1" 400 122 0.000295

Adding cors_allowed_origins=[] to the server resolves the issue. This fix is a little dubious but acceptable

0reactions
miguelgrinbergcommented, May 8, 2020

Use cors_allowed_origins='http://localhost:8080' for increased security.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting connection issues | Socket.IO
You are trying to reach a plain WebSocket server; The server is not reachable; The client is not compatible with the version of...
Read more >
Flask-socketio issues with https, http, wss unable to connect in ...
In io.connect on the client side, the connect parameter used https:// + document.domain + location.port but the server was using http:// so ...
Read more >
Flask-SocketIO — Flask-SocketIO documentation
Flask -SocketIO gives Flask applications access to low latency bi-directional communications between the clients and the server. The client-side application ...
Read more >
Create a Secure Chat Application with Socket.IO and React
You should not receive any error messages but you also will not see much at this point because the server is not connecting...
Read more >
Implementing WebSocket communication in Next.js
Creating the server side ;, which does not exist by default and shows that the socket connection is not in place. If this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found