getting socket id of a client in flask socket.io

Is there a way to get socket id of current client? I have seen it is possible to get socket id in node.js. But it seems flask's socket.io extension is a little bit different than node's socketio.

From Flask-SocketIO documentation:

The request object defines request.namespace as the name of the namespace being handled, and adds request.sid, defined as the unique session ID for the client connection

Welcome to Flask-SocketIO's documentation!, For WebSocket support in gevent, uWSGI is preferred, followed by Flask-​SocketIO also supports SocketIO namespaces, which allow the client to All clients are assigned a room when they connect, named with the session ID of the To get the list of rooms a client was in the 0.x release required the application to use a  I want to get data in my Flask server from a socket.io client. My setting is: Client1 calls method request_data on server. The method request_data then emits 'client2' event on Client2. I have the

I found it is possible in flask too.

In the flask's request object there is a socket object which has an id.

from flask import request
# ....
@socketio.on('connect')
def onConnect():
    currentSocketId = request.namespace.socket.sessid
# ....

Flask-socket.io emit to specific clients · GitHub, Flask-socket.io emit to specific clients. If I can get the ID of this room, then all I need to do is store that ID somewhere, and emit to it using: (I'm using quotes  Flask-socket.io emit to specific clients. GitHub Gist: instantly share code, notes, and snippets. defined as the unique session ID for the client connection, and

Consider using request.sid which gets its value populated from the FlaskSocketIO library.

Emitting to specific client · Issue #89 · miguelgrinberg/Flask-SocketIO, a) socket.emit b) emit to the room named "Socket#id" where id is a unique client id. I was wondering how this can be achieved with Flask-socket.io? Would option (b) apply? If so how can I obtain the client id? Best regards,. I'm trying to create a python app that can send message from server to client. Currently I'm using this sample code from here. to client using Flask-Socket IO

Socket.IO, IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths. Get started · Documentation · Demos · Server API · Client API IO clients connect to by default, and the one the server listens to by default. IO is identified by a random, unguessable, unique identifier Socket#id . Each Socket in Socket.IO is identified by a random, unguessable, unique identifier Socket#id. For your convenience, each socket automatically joins a room identified by this id. You emit to a room like: io.to('some room').emit('some event') If you want to emit to just a specific socket.id, just replace 'some room' with the associated id.

Socket.IO, EIO=3&transport=polling&sid=<id> (the namespace is sent as part of the payload​). With query parameters. const socket = io('http://localhost?token=abc'); flask_test_client – The instance of the Flask test client currently in use. Passing the Flask test client is optional, but is necessary if you want the Flask user session and any other cookies set in HTTP routes accessible from Socket.IO events.

Socket.IO, 1, 2, 'abc'); // sending to all clients except sender socket.broadcas. WARNING: `​socket.to(socket.id).emit()` will NOT work, as it will send to everyone in the  A much more efficient way would be to access the socket by id through a library (an object where id is the key). From the source, another way to access a socket by id is io.sockets.socket(id);. Unfortunately, there are 2 problems here: the method is flagged private; the method creates a new socket when the given id does not exist

Comments
  • Not recommended, this is an internal data structure that may change in the future. A better alternative is to create your own identifiers based on user information you have in the session, for example.