How to best implement play with random palyers

I have a scenario where one user would like to play game with someone random online.
Here are basic requirements

  • select one random user
  • send a request to that user via channel
  • if user doesnt respond within 10 seconds, try to connect to other user
  • if user accepts within 10 seconds, stop sending requests and start the game
  • if user rejects the game request, lets say after 2 seconds search for another user

Here is what I have tried:

  • I start a task ie Task.async -> search_palyer end
  • search_palyer will first check if the last request was accepted or rejected
  • if last request was not accepted, it will fetch another random player and send request
  • called timer.sleep for 10 seconds
  • recursive call to same function untill it succeeds

This works but the problem is, if the palyer to which I send request rejects immediately my task will sit idle for 9-10 seconds and wont do anything. I want to find player as soon as possible. Player gets a bad experience this way as he/she has to wait longer

1 Like

Can you use pubsub instead of task?

  1. pick a random free player
  2. subscribe to their channel
  3. send “request” to their channel with a ref.
  4. other player can accept or reject by sending accept or reject with the ref onto their own channel.
1 Like