Skip to content Skip to sidebar Skip to footer

Registering Server Events With Flask Socketio

I'm getting started with flask and SocketIO using https://github.com/miguelgrinberg/Flask-SocketIO. I want to post a string to the flask server and then via SocketIO, emit this to

Solution 1:

You're right with your assumptions. First, POST the data to Flask:

.ajax({
    url: "{{ url_for('index') }}",
    method: "POST",
    data: {
        token: "value"
    }
});

Your view would look like

@app.route('/index',methods=['POST'])defindex():
    token = request.form['token']
    test_message(dict(data=token))
    return'1'

And your JavaScript would look something like

var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
socket.on('connect', function() {
    socket.emit('my event', {data: 'I\'m connected!'});
});
socket.on('my response', function(msg) {
    // do something with msg.data
});

Post a Comment for "Registering Server Events With Flask Socketio"