0

This is in base_manager.py in socket_io python server--> How does this code make any sense? I want to emit a message. How could that ever work if there are no rooms? If namespace not in self.rooms--> there is no room so it will always return back to caller right?:

 def emit(self, event, data, namespace, room=None, skip_sid=None,
         callback=None):
    """Emit a message to a single client, a room, or all the clients
    connected to the namespace."""
    if namespace not in self.rooms or room not in self.rooms[namespace]:
        return
    for sid in self.get_participants(namespace, room):
        if sid != skip_sid:
            if callback is not None:
                id = self._generate_ack_id(sid, namespace, callback)
            else:
                id = None
            self.server._emit_internal(sid, event, data, namespace, id)
KonradS
  • 91
  • 1
  • 5

1 Answers1

0

Note that you are looking at an internal method, not something that is accessible to applications.

The piece of information that you are missing is (I think) that all users have a room of their own. The way you send a message to a single user is by sending it to that user's room, which is named with the sid that was assigned to that client session.

So, say you have a user that was assigned sid="12345". In your application, you can send a message to this user by calling emit('some-event', data, room='12345').

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152