Nick
Nick

Reputation: 406

React socket.io-client with Python flask_socketio not handling events

I am implementing websockets on my app. The connection occurs but the echo event never triggers and I can't see why. The server serves my index.html successfully and the "Connected" line does log on my server and I'm not getting any 404s or refused connections on my front end or errors on my server.

Client code:

componentDidMount() {
        const socket = socketIOClient(ipaddress)
        socket.on('connection', () => socket.emit('echo', { data: 'hello' }))
        socket.on('echoresponse', ({ data }) => {
            console.log(data)
            socket.disconnect()
        })
}

Server code:

app = Flask(__name__,
                static_url_path='', 
                static_folder='dist',
                template_folder='dist')

socketio = SocketIO(app)

@app.route('/')
def home():
    return app.send_static_file('index.html')

@socketio.on('connect')
def connected():
    print('Connected')

@socketio.on('disconnect')
def disconnected():
    print('Disconnected')

@socketio.on('echo')
def echo(message):
    emit('echoresponse', {'data': message['data']})

socketio.run(app, host='0.0.0.0', port=80)

Upvotes: 1

Views: 1385

Answers (1)

Nick
Nick

Reputation: 406

It was a typo. In my client-side code I wrote 'connection' when I should have written 'connect'.

Upvotes: 1

Related Questions