Reputation: 11
I'm trying to build a Flask server in Python that uses socket.io. As I have never used socket.io before I used the code from Miguel Grinberg to make a simple website to test if I can get it working. When I navigate to the website and then check the server-console I see requests coming in to "/socket.io/?EIO=4&transport=polling&t=<some code>" that are responded to with status code 200.
However, in my browser (Chrome) the console shows an error: "Uncaught DOMException: Failed to read the 'responseText' property from 'XMLHttpRequest': The value is only accessible if the object's 'responseType' is '' or 'text' (was 'arraybuffer')." screenshot
It seems to me like when calling io.connect in my javascript a request is made to the server but somehow the response or the processing of it fails which results in a failed connection.
Does anyone have any idea for why that happens and how to fix it?
My server side code is
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('my event', namespace='/test')
def test_message(message):
emit('my response', {'data': message['data']})
@socketio.on('connect', namespace='/test')
def test_connect():
emit('my response', {'data': 'Connected'})
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
print('Client disconnected')
if __name__ == '__main__':
socketio.run(app)
The Javascript running on the client side is
$(document).ready(function(){
var socket = io.connect('http://' + document.domain + ':' + location.port + '/test');
socket.on('my response', function(msg) {
$('#log').append('<p>Received: ' + msg.data + '</p>');
});
$('form#emit').submit(function(event) {
socket.emit('my event', {data: $('#emit_data').val()});
return false;
});
});
Upvotes: 1
Views: 902
Reputation: 31
I recently ran into the same problem.
It seems that flask-socketio is compatible with Socket.IO 2.x but not with Socket.IO 3.0. In my case, downgrading the JS library on the client side solved the problem.
Upvotes: 2