0

I have problem regarding socket communication between fully working Flask web app and python client. I must say that in web browser everythig works, so I need Python equivalent.

so, in Flask app i have this:

from flask_socketio import SocketIO, emit
socketio = SocketIO(app, manage_session=False)

.....................

@app.route('/testing')
def socket_test():
    msg='hello!'
    socketio.emit('worker_msg', msg)

In client app i have this:

import socketio

# standard Python
import socketio

sio = socketio.Client()

@sio.event(namespace='/testing') # this works
def connect():
    print('connection established')

@sio.event(namespace='/testing') # this is not working
def worker_msg(msg):
    print('message received with ', msg)

@sio.event(namespace='/testing') # this is working
def disconnect():
    print('disconnected from server')

sio.connect('http://address:8000', namespaces=['/testing'])
sio.wait()

Idea is to connect to a namespace on server, and on client side to receive some custom events. I have tested various combinations in naming functions on client side, with no luck. I think that problem lies in faulty event naming on client side.

In javascript i have something like this, and it works as expected: socket is connected to namespace"/main" and it expects event "main_update" (with some data payload)

$(document).ready(function(){
    var namespace = '/main';
    var socket = io(location.protocol + '//' + document.domain + ':' + location.port + 
namespace);
    socket.on('main_update', function(msg) {
        var data = JSON.parse(msg);
        $('#btn1').attr('class', data.status_1);
        $('#btn2').attr('class', data.status_2);
        $('#btn3').attr('class', data.status_3);
        $('#btn4').attr('class', data.status_4);     
        $('#btn5').attr('class', data.status_5);
        $('#btn6').attr('class', data.status_6);
    });
});
Ivica
  • 29
  • 4

1 Answers1

0

ok... my bad, I wasn't careful enough while reading docs and examples.....

solution is simple as allways, when you figure it out:

@app.route('/testing')
def socket_test():
    msg={'data' : 'hello!'}
    socketio.emit('worker_msg', msg)

message soposed to be a dict!

Ivica
  • 29
  • 4