Generate unique room code to share with friends

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Generate unique room code to share with friends

Iggy
2021-02-25 21:13:25

Hello, I was curious is there a way for PUN to generate a unique secret code out of 4-5 numbers that you can share with your friends?

If anyone has played phasmophobia you'd know what I mean. When you create a room you don't have to go through the bureaucracy of giving your room a name and a password. You get an automatically generated secret code that you can share with your friends, which they use to join you.

I could randomize the number from 0-99999 but there's no guarantee that it will be unique. Do I have to write server code or is there another, better way?

Thanks :)

Comments

JohnTube
2021-02-26 10:04:36

Hi @Iggy,

Thank you for choosing Photon!

You could do this in multiple options:

of course preferably a server generates the codes (via custom plugins) but it's not an option for everyone (self-hosted or Enterprise Cloud)
here is an example using webhooks + web based backend

otherwise to minimize clash and make this based on client only:
make the random number generation takes input timestamp and maybe room creator's UserID (add region, AppVersion, etc.)
try CreateRoom if it fails with GameAlreadyExists retry.

Iggy
2021-03-01 16:43:35

Thanks for the help @JohnTube , I've managed to write a little server in GO and host it on Heroku.

I followed Oddreams instructions and it worked really nicely. But I also found that you can optimize the number of requests if you make the host player get the code before creating the room, and assign it to the name of the room. That way everyone who joins doesn't need to send a request for conversion of code => room name.

Of course, that creates an issue where you can allocate a ton of codes on the custom API without actually creating a photon room (Photon error, Postman spamming). My solution here is to timeout codes that didn't get a matching PathCreate webhook call.

Current implementation:

  1. User connects to the Photon Server.
  2. User presses a button to create a new room.
  3. We send a request for a unique code from the custom API.
  4. Custom API generates a code, stores it in the map, starts a timeout for it, and sends it to the user.
  5. We make a call to create a Photon room with the name set to the received code.
  6. Custom API catches the PathCreate webhook, and cancels the timeout of this code.
  7. Another user joins the game by specifying the code as the room name and is successful in joining.
  8. Both players leave and the room is closed.
  9. Custom API catches the PathClose webhook, and removes the code from the map.

Kaiserludi
2021-03-02 18:14:12

@Iggy, @JohnTube.

If the 4-5 numbers are not a hard requirement, then the easiest actually is, to not let the room creator specify a room name. The server will then generate a GUID for the room name.
This GUID is 32 characters long, so considerably longer than the 4-5 characters that were mentioned in the question, but it is guaranteed to be unique and you neither have to write custom server side code, nor to host the server yourself or use the Enterprise Cloud, nor do you have to generate a random number on the client side.
The room creator can specify at room creation time that the room should be invisible. That way the room name is secret and only known by the creator and by anyone to whom the creator sends that room name

Iggy
2021-03-02 21:39:57

@Kaiserludi Oh interesting, that could definitely work well if you utilize the clipboard. I guess you have to pick your battles.

For anyone that needs help, I uploaded my code to GitHub.

Back to top