I used the MQTT library to make a small online game.
CONTROLS:
Arrow keys or WASD to move the player. The green player is you.
GOAL:
Kill the other player. Just shoot the enemy (red square) enough times.
MATCHMAKING:
It might take a while to find someone to play with (if someone is online at all). Just wait. The screen might flicker a few times. That's normal.
I played a few times and every time I lost
Does that mean that someone else was actually competing, or will the game spawn a bot adversary if it takes too long to wait for a competitor?
Listen to the topic for a while (find other players waiting for people to join)
If the system finds a player
Send a message to the server ("server" being the person who created a new "room"; you'll know if you were the server owner if you spawned in on the left side) indicating that a player is joining
Both computers agree to meet each other at a specific time (the black screen; your computer is waiting to start at the time specified)
If one of the players does not connect in time (usually a severe error and one of the players has to quit, or a hacker was detected and kicked from the game) then the other player will move back into the matchmaking phase
If no players were found
Create a new "server" (can be joined when another player is at the "listening" phase)
Wait for other players to join
If people still do not join, start a game with a bot
If the game starts but the game keeps erroring out (usually a hacker, on normal circumstances you'll usually only get one when you first start the game, I don't know why)
Spawn a bot
I plan to make it so that if you can beat the bot its aim will slowly get worse and worse.
Also, when you are in a match this is what's happening behind the curtains:
When the game starts, both players send "packets" indicating their position, mouse position, etc.
When you receive a packet, some variables are updated on the enemy's velocity and gravity. The game will predict where the player is while it waits for the next packet (usually around every 0.3 seconds a new one is received). That's why if you are playing with a real player, you'll occasionally see people fall off edges and magically teleport back to the top of the edge.
If you wanted, you could make a great tutorial on client/server programming by taking this program and tearing it apart, with snippets of code followed by text explaining each snippet.
PS That big comment used to also be in the project notes, which are now empty!
It's more like creating a room and having a player join it, I just called it a "server" because one client hosts this room and allows other players to discover that room.