Muatik
Muatik

Reputation: 4171

get current connection in flask socket.io

I want to make multiple emitting to an individual socket connection using flask's socket.io extension.

from flask.ext.socketio import SocketIO, emit, join_room, leave_room


# creating flask app and io...

@io.on("/saySomething")
def saying():
  emit("said", "hello")
  saying2()

def saying2():
  # ...
  # doing something long and important
  # ...
  emit("said", "and how are you?")

I do not know which connection saying2 is emitting to. Should I pass current connection to saying2 method? How can I achieve my goal?

Upvotes: 0

Views: 1269

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67479

In your example, saying2() is emitting to the client that sent the /saySomething event. This is based on a concept similar to request contexts in standard Flask.

The emit function has two optional arguments to send to other clients:

  • broadcast=True will send to all connected clients, including the client that sent the /saySomething event.
  • room=<room-name> will send to all clients attached to the given room. If you want to address individual clients, then put each client in a different room and target the desired room.

Upvotes: 1

Related Questions