Photon Realtime is our base layer for multiplayer games and higher-level network solutions. It solves problems like matchmaking and fast communication with a scalable approach. It is used by games and our more specific multiplayer solutions PUN and Quantum.
The term Photon Realtime also wraps up our comprehensive framework of APIs, software tools and services and defines how the clients and servers interact with one another.
These pages are primarily the manual for the client-side Realtime APIs but will also provide overview of the structures that are involved.
Clients And Servers
The Photon Realtime API is the client-side software which will be directly used by games and apps. With it your apps
RaiseEvent for example.
It is available in various languages for most of the popular platforms and engines (SDK Download Page) and pretty much all clients can interact with each other, no matter if iOS, Android, web, console or standalone.
All Photon Realtime clients connect to a sequence of dedicated servers, split by three distinct tasks: Authentication and regional distribution (Name Server), matchmaking (Master Server) and gameplay (Game Server). As those servers are handled by the Realtime API, you don't have to worry about them but it's good to have some background.
The Photon Cloud is a fully managed service, which provides worldwide hosting for photon realtime clients. A free Photon Account gives you access to it without obligation and we recommend to use the Photon Cloud while you get used to Photon Realtime.
Below are a few code samples to give you an idea of how the Realtime API is being used. Consider this an overiew but not a complete, working guide.
Use the Photon Cloud and skip setting up servers. You will need to set an AppId and region code (e.g. "eu" or "us") in the code. Here is the list of available regions.
Get your AppId from the Realtime Dashboard after free signup.
Connect To Photon Cloud
The following code is a class that will connect to the Photon Cloud, if you fill in your appid.
Connect To Self-hosted Photon Server
Connecting to your self-hosted Photon Server does not require AppId or region. All you need is the server address and port to use with the appropriate connect method of your client.
You can read about the differences between Photon Cloud and Photon Server here".
The LoadBalancing API is built to integrate well with your game logic.
You can fine-control when you want to handle incoming messages and how often you send anything.
Internally, messages in both directions are buffered until your game calls
Games often use a game loop that calculates updates and then refreshes the screen.
Service() 20 to 50 times per second depending on your game and network usage.
If you don't call it, no "network progress" gets done at all.
Service covers two tasks:
- Received events and data gets executed.
This is done when you can handle the updates.
As a sequence order is always kept intact, everything that the client receives is queued and ordered.
DispatchIncomingCommandsto do this task.
- Outgoing data of your client is sent to the server.
This includes acknowledgments (created in the background) which are important to keep the connection to the server.
SendOutgoingCommandsto do this task. Controlling the frequency of
SendOutgoingCommandscalls controls the number of packages you use to send a client's produced data.
When the application is quitting or when the user logs out do not forget to disconnect.
Create A Game
To create a new room, aka game, invoke "Create Room" operation on your connected LoadBalancing client.
This sets the room name and amount of players that are allowed in the room. The client will enter the new room automatically. When using the "Create Room" operation the room will be created before joining, if it doesn't exist. Rooms exist until the last player leaves.
You can define "Custom Room Properties" to set shared values for the room when you create it. The custom room properties can be used (e.g.) to store the map name, level or the round duration. The keys of Custom Properties have to be strings. Of course, those values can be set and modified in the room as well.
You can select any of your custom properties to be shown also in the lobby, by setting the optional array with their names as "Custom Room Properties For Lobby". Properties showing in the lobby can be used for matchmaking and as a filter for random matches.
Read more about matchmaking in our guide here.
Find A Game
Clients join games by name or ask Photon to find a perfect match.
Find rooms ...
- Random: Matches players randomly. Optionally fills rooms or distributes players evenly.
- Parameterized: Customize random matching by defining lobby properties as filters.
- Private: Hide rooms and players can only join if they know the room name.
- Listing: The lobby lists visible rooms to let players pick and join manually.
With Photon Realtime, rooms' data can be saved and loaded easily. You need to set up webhooks to hook up Photon Cloud with an external web server.
Once set up, room states will be saved automatically for you. To rejoin rooms:
This feature makes asynchronous matchmaking and gameplay possible.
Read more about how to do this in our persistence guide.
Whatever happens on one client can be sent as an event to update everyone in the same room.
Update your players with stats, positions or your current turn. Photon will send it as fast as possible (with optional reliability).
- Send messages/events: Send any type of data to other players.
- Player/Room properties: Photon updates and syncs these, even to players who join later.
Your event codes should stay below 200. Each code should define the type of event and the content it carries.
The event data in the example above is a
It can be a
byte or any data type supported by Photon's serialization (a
See serialization in photon for more information.
Whenever an event is dispatched a handler is called. An example is shown below.
Each event carries its code and data in the way your client sent them. Your application knows which content to expect by the code passed (see above).
For an up-to-date list of default event codes look for the event codes constants in your SDK, e.g. within
ExitGames.Client.Photon.LoadBalancing.EventCode for C#.
Custom Or Authoritative Server Logic
As is, without authoritative logic, Photon Cloud products already allow for a broad range of game types.
- First Person Shooters
- Racing Games
- Minecraft type of games
- Casual real-time games
- Asynchronous and synchronous games
Use photon server or photon plugins to implement your own custom logic.