Advertisement
Guest User

web-sockets

a guest
Jul 7th, 2023
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. Websockets have implemented a keep alive mechanism using ping pong.
  2.  
  3. going by https://www.rfc-editor.org/rfc/rfc6455.html
  4.  
  5. section 5.5.2 and 5.5.3 tell us about what ping and pong are.
  6. Now we are supposed to expect a pong message with same data which was sent in the ping message.
  7. In websockets library in python , they follow the same.
  8. The steps that are taken are(avoiding ping_interval and ping_timeouts):
  9. * Generate a unique data (bytes) , map it to a dictionary and assign a asyncio future to it(consider it as a task).
  10. * Send the Ping with that uniquely generated data.
  11. * Now we wait for the client to respond back with a pong message with the "same data" , and if we receive it within 20 seconds(default keepalive timeout) we keep the connection alive , else we close the connection.
  12.  
  13. Now the question i have is why instead of relying on unique message , we rely on whether the client to which we sent a ping has responded with a pong irrespective of the data , if it is sending us something that means the connection is alive.
  14.  
  15. Recently i had to create a hybrid ping pong mechanism which uses the client which is connected to my websocket server, i use it's id to map it to the ping task and if i receive any pong from that particular client id i mark the connection as keep alive.
  16.  
  17. In normal ping pong, if a client responds with a different message in pong , it doesnt acknowledge it and marks the connection as closed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement