realtime | current

Realtime Intro

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.

If you develop with the Unity engine, consider using [Fusion](/fusion) or [Quantum](/quantum), which both help synchronizing game state and simulation properly.

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 Connect, JoinRandomRoom and 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 is good to know the 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.

Code Samples

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 to skip setting up servers. You will need to set an AppId in the code.

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".

Call Service

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 LoadBalancingClient.Service().

Games often use a game loop that calculates updates and then refreshes the screen. Call 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 the sequence order is usually kept intact, everything that the client receives is queued and ordered. Service internally calls DispatchIncomingCommands to hand over available messages one by one.
  • 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. Service internally calls SendOutgoingCommands to do this task. Controlling the frequency of SendOutgoingCommands calls 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 (a.k.a. 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 15 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 as filter for random matchmaking.

Read more about matchmaking in our guide here.

Find a Game

Clients join games by name or ask Photon to find a 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.

Persist Games

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.


Sending Events

Whatever happens on one client can be sent as an event to update everyone in the same room.

Update your players with positions, your current turn or state values. 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 defines the type of event and the content receivers can expect.

The event data in the example above is a Hashtable. It can be a byte[] or any data type supported by Photon's serialization (a string, float[], etc.). See Serialization in Photon for more information.

Receiving Events

Whenever an event is dispatched a handler is called. An example is shown below.

Each event carries the code and data your clients define and send. 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.

To Document Top