0

I'm trying to setup my Flask app to use RabbitMQ as a message queue. It works fine if I emit messages from the server but if I try to emit messages nothing happens. It seems like the front end socket is not communicating with the queue.

My socket code looks as follows:

from flask_socketio import emit, SocketIO

socketio_mp = SocketIO(message_queue='amqp://guest:guest@localhost:5672//')

@socketio_mp.on('connected', namespace='/test')
def joined():
    """Sent by clients when they enter a room.
    A status message is broadcast to all people in the room."""
    print('connected')

The socket is initialized correctly as shown in the Flask-SocketIO docs. The connected event is never triggered even though it is emitted from the front end.

var namespace = "/test";
socket = io.connect(location.protocol + "//" + document.domain + ":" + location.port + namespace);

socket.on("connect", function() {

    console.log("connected");
    socket.emit("connected", {msg: "next"});
});

I also get no errors in the console.

Neill Herbst
  • 2,072
  • 1
  • 13
  • 23
  • The provided code does not show how you're running the app. As stated on the Flask-SocketIO docs, you should run the app via `socketio.run()` wrapping the Flask `app.run()`. – farzad Jun 02 '17 at 10:16
  • @farzad I actually do initialize the app the right way. The app works when the message queue is not added. – Neill Herbst Jun 02 '17 at 10:25
  • The issue address here https://github.com/miguelgrinberg/Flask-SocketIO/issues/248 – Raja Simon Jun 02 '17 at 11:08

1 Answers1

1

When you create your SocketIO instance, you have to pass your Flask app as a first argument:

socketio_mp = SocketIO(app, message_queue='amqp://guest:guest@localhost:5672//')

As a side note, I'm not sure what you expect to be different when you add a message queue in this way. The clients never talk to the message queue. The queue is used internal when you have multiple Flask-SocketIO servers or emit-only processes. Clients do not have direct access to the queue.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • I'm using a blueprint style Flask app. Will it still work if I use the `init_app` method? I'm just not sure why when I emit an event on the frontend it is not picked up by the flask backend. And this only happens when I add the message queue. Even the built-in events like `connect` don't fire on the backend. – Neill Herbst Jun 05 '17 at 08:18
  • Sure, you can use `init_app()` as well. – Miguel Grinberg Jun 05 '17 at 15:59