1

I use the python socket.io client and I would like to know if it is possible to retrieve the session cookie, if so, how?

Another question, is it possible to get the answer from an .emit without going through an @sio.event or @sio.on()? As with the websockets library:

websocket.send('Hi')
response = websocket.rcv()
print(response) -> "Hi :)"

If not, is it possible to create an event/on that retrieves messages from the server that do not contain an event name?

For example I send from the client: sio.emit("GetNbrPlayers") but the server answers me [{"data": "5"}] without an event before the data (it is a server/api that uses socket.io too), I would like to get this message but impossible with an event/on because there is no event name in the answer.

Thank you in advance!

Peter_Pan
  • 19
  • 2

1 Answers1

0

it is possible to retrieve the session cookie,

What do you mean by session cookie? If you mean something like the Flask session cookie, then no, there is no session cookie. The user session is stored in the server, not in a cookie.

If you mean the sid cookie that contains the session id assigned to the client, you can obtain this id from the sid attribute of your client object, for example as sio.sid.

is it possible to get the answer from an .emit without going through an @sio.event or @sio.on()?

Yes, you can use the "ack" feature of the Socket.IO protocol for this. If the server is a Python server, you can just return the response from your handler function. For example:

@sio.event
def GetNbrPlayers():
    return [{"data": "5"}]

In the Python client you have two ways to receive this information. You can use a callback function:

def get_response(data):
    print(data)

sio.emit("GetNbrPlayers", callback=get_response)

Or you can use call() instead of emit() to combine the emit and the callback handling into a single function call:

data = sio.call("GetNbrPlayers")
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152