8

In my app, I need the client to join a room so that it may then receive messages from my server.

Server Code

@socketio.on('join', namespace='/test')
def join(message):

    join_room(message['room'])
    room = message['room']
    emit('my response', {'data': 'Entered the room ' + message['room']}, room=room)


@app.route('/scan/user/<int:user_id>/venue/<int:venue_id>', methods = ['POST'])
@auth.login_required
def scan_tablet_user_func(user_id, venue_id):

    room = 'venue_' + str(venue_id)
    socketio.emit('my response', {'data': json.dumps(my_info, ensure_ascii=False)}, room=room)

Client Code

$('form#join').submit(function(event) {
            socket.emit('join', {room: $('#join_room').val()});
            return false;
        });

As soon as the webpage loads, I enter "venue_1" into the webpage form entitled "join".

The variable room on the server side is also set to "venue_1".

The issue is that when I call the API /scan/user/..., nothing appears on my client. However,

emit('my response', {'data': 'Entered the room ' + message['room']}, room=room)

does appear correctly.

1 Answers1

5

I think that you need to supply a namespace parameter to your emit in the API call, since you have defined a namespace.

socketio.emit('my response', {'data': json.dumps(my_info, ensure_ascii=False)}, room=room, namespace='/test')

If you don't supply the namespace you will use the default one.

Zyber
  • 1,034
  • 8
  • 19
  • 2
    Correct. When you reply to an event sent by the client, the namespace is obtained from the client event. When the server is the initiator, that context does not exist, so you have to provide the namespace. – Miguel Grinberg Jul 03 '15 at 18:41