I am writing a matchmaking script for a game through a web portal. For the past few days I have been looking into the different options and I believe the following approach would be the most optimal but I would like the opinion of others.

  1. Get players match requirements through a web form (Method GET) and place the player in queue (mySQL database entry with time stamp and match requirements) for 1 hour.
  2. On the submit page run an ajax script every minute that contacts a php script that checks the following:
    • Is a player still in the system, if their hour is over or their ajax script has not ran in 5 minutes remove them from the database. Return time out.
    • Is the player in queue flagged for a match?
      • If yes then place both players in the in match table and remove both players from queue and send match variables.
      • If no then continue.
    • Do the remaining players match the current players requirements?
    • If yes:
      • Is the player the same one looking for a match.
      • If no: Return match variables. Flag other player.
      • If yes: Return match not found.
    • If no: Return match not found.
  3. When match variables are received the page will be updated with jquery, including a new ajax request that will call a new php script every 30 seconds to find out if both players accept the match. Possible ajax results are: 0 waiting, 1 accepted, 2 declined. There will also be a button that will send the players response immediately to the php script through ajax that will disable itself through jquery when pushed. When both players accept the page will then use jquery again to display the instructions for beginning the match.

Is there a cleaner or less intensive way to do this with or without involving the database and php?

No code is written yet.
The system will not have more than 100 players in queue at any time. (Most likely a peak of 25)

2 Answers 2


Since you woun't have that much players, I wouldn't recommend on spending too much time on this issue, but continue on your other services.

I would use an AJAX script that scans off a php script every second, where the PHP script is looking for an opponent.

How would it work? - A "Find match" button calls an external PHP script via AJAX

  • The PHP script checks if the player with the matching login hash is in a queue, if not add the player, then search for an opponent, and return the results
  • If the player is in the queue (On second or more calls), it skips the queue registration and immediately starts searching for an opponent
  • If a match is found, create a "matchfound"-table with various states, such as "waiting for player 1" / "waiting for player 2" / "player 2 declined" etc
  • With this information being sent to the 2 clients each second, you just pass on the current state in json to the clients, where you then have your presentation code (with for example a turning wheel when waiting for opponent etc)

This system would support alot of players, but if you're aiming on creating a large match making system with thousands of players simultaneously, you should consider looking into other solutions then JavaScript/PHP, where socket-usage would be the best solution.


I suggest you look into websockets (like pusher.com or it's open source variant slanger or socket.io). It allows you to keep connections with clients open and thus see whether players are still active or not. It removes your need for ajax calls to any backend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.