1

Do python-socketio or underlying python-engineio have any kind of confirmation that specific message was completely delivered to other side, similar to what TCP does to ensure all data was successfully transferred to other side?

I have kind of pubsub service built on python-socketio server, which sends back ok/error status when request has been processed. But in my python-socketio client sometimes I just need fire and forget some message to pubsub but I have to wait it was completely delivired before I terminate my application.

So, my naive code:

await sio.emit("publish", {my message})

it seems the await above is just scheduling send over wire to asyncio, but does not wait for send to complete. I suppose it's by design. Just need to know is it possible to know when send is complete or not.

bialix
  • 20,053
  • 8
  • 46
  • 63
  • 1
    The only way to get a reliable ACK is when the *process* on the other side sends it, i.e. it must be a part of the communication protocol. – VPfB Dec 01 '22 at 10:10

1 Answers1

1

Socket.IO has ACK packets that can be used for the receiving side to acknowledge receipt of an event.

When using the Python client and server, you can replace the emit() with call() to wait for the ack to be received. The return value of call() is whatever data the other side returned in the acknowledgement.

But not that for this to work the other side also needs to be expanded to send this ACK packets. If your other side is also Python, an event handler can issue an ACK simply by returning something from the handler function. The data that you return is included in the ACK packet. If the other side is JavaScript, you get a callback function passed as a last argument into your handler. The handler needs to call this function passing any data that it wants to send to the other side as response.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Bot sides are python-socketio. – bialix Dec 01 '22 at 12:00
  • I just wonder will such ACK be any different from my current situation when server emits back some answer to me? – bialix Dec 01 '22 at 12:02
  • 1
    The ACK packet is very much like the EVENT packet, with the exception that it contains a reference to the originating event. If the other side replies with a standalone event, then there is nothing that links the event with its response, so it would be up to your application to interpret the responding event as a response, for example based on data included in the payload. – Miguel Grinberg Dec 01 '22 at 19:21
  • 1
    In case you are interested, here is the reference docs for the EVENT and ACK packets in Socket.IO: https://github.com/socketio/socket.io-protocol#2---event – Miguel Grinberg Dec 01 '22 at 19:23
  • Thanks a lot, you shed some bright light on the underlying protocol. Previously I had no idea what is the proper idiomatic way for building typical request-answer protocol with socketio. Now that all becomes more clear. – bialix Dec 01 '22 at 22:21