Sending messages directly from client to client
The whole answer can be found below.
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).
Sending messages directly from client to client
Steven
2021-04-30 19:12:07
Hello, it is my understanding that any time RaiseEvent is used to send a message from one client to one or more other clients, this message first goes to a photon server, and then on the the recipient(s). First, is this always true? If so,
- is there a way within the photon system to send direct udp messages between clients without going through a relay server?
- if not, is there a way to use photon to get the information needed (ip address etc) to set up a udp listening server in C# apart from photon.
My goal is to use photon for almost all communication between clients in the game, but while a match is going on, I want to send unreliable udp messages directly between clients regarding position, rotation, speed, etc. Skipping the relay server would cut the latency in half.
Has anyone tried setting something up like this?
Comments
Kaiserludi
2021-04-30 20:01:56
Hi @Steven.
First I need to clarify something:
Skipping the relay server would cut the latency in half.
This is a misconception.
If the latency between client A and the server is 50ms, the one between client A and client B is 200ms and the one between client B and the server is 50ms as well, then the relayed connection would actually only have half the latency of the direct connection.
On the other hand if the latency between client A and B is less than 1ms (both are on the same local network), but the latency between either of them and the server is 100ms, then a direct connection would cut the latency to less than 1 percent.
Conclusions:
If a direct connection improves latency, and if so, then by how much, strongly depends on the individual circumstances and there is no general rule of thumb regarding this matter.
Actually relayed connections are usually superior to direct p2p connections as they don't have NAT issues and as in games with more than one player, the rest of the players can still communicate and continue playing, when the host loses its connection.
Hello, it is my understanding that any time RaiseEvent is used to send a message from one client to one or more other clients, this message first goes to a photon server, and then on the the recipient(s). First, is this always true? If so,
- is there a way within the photon system to send direct udp messages between clients without going through a relay server?
- if not, is there a way to use photon to get the information needed (ip address etc) to set up a udp listening server in C# apart from photon.
Direct p2p connections between clients are NOT supported by PUN nor by Photon Realtime C# clients, neither do they offer a way to access the IPs of the local client or of other clients.
Direct 2p2 connections between client ARE supported by Photon Bolt Pro and by Photon Realtime and Photon Voice C++ clients.
So your options are:
a) stay with PUN and use relay-only communication
b) switch to Photon Bolt Pro (requires a monthly subscription)
c) switch to the Photon Realtime C++ Client SDKs (and access them via native Unity plugin written by yourself)
@Kaiserludi
Thanks for the info! Are there any example projects or tutorials for photon realtime c++?
@Kaiserludi
Also, which c++ sdk would I use for this? I see c++ sdk's for specific platforms, but not a general c++ sdk.
Kaiserludi
2021-04-30 20:32:47
@Steven:
Yes, there are various example projects included in the 'Demos' folder within the Client SDKs.
However those are not Unity native plugin examples, but plain C++ projects.
There are no example projects available for on using the C++ Client libs from inside of Unity.
Some developers successfully use the Photon RealtIme C++ Client from within Unity, so it is definitely possible, but you should have some experience with C++ and you should be prepared to have to learn via the Unity docs how to write and use Unity native plugins.
If you are a beginner programmer, then this road will certainly be more difficult than just using PUN.
Kaiserludi
2021-04-30 20:37:51
@Steven wrote: »
@Kaiserludi
Also, which c++ sdk would I use for this? I see c++ sdk's for specific platforms, but not a general c++ sdk.
You would use the SDKs for all the platforms that you are targeting.
Unity native plugins generally work on a per platform basis: You explicitly tell Unity via the Unity inspector for which target platform your plugin is. You would then write a separate unity native plugin per target platform, with each of those plugins using a different Photon C++ Client SDK. All those plugins can then expose the exact same APIs to Unity, so that your C# code could stay identical across all platforms.
@Kaiserludi
Thanks again for the info!
One more question: In the Photon C++ Client API documentation, can you point me to the functions involved in connecting clients directly and sending messages?
Kaiserludi
2021-04-30 21:04:29
@Steven:
You just specify a 'DirectMode' (https://doc-api.photonengine.com/en/cpp/current/a04814.html) that differs from the default value of 'NONE' via the RoomOptions on room-creation and the Photon clients will automatically establish the direct connections to each other whenever a new client is joining the room.
Inside the room you then use opRaiseEvent() to send a message via the relayed connection, but sendDirect() (https://doc-api.photonengine.com/en/cpp/current/a05522.html#a53309df76f7e0b3d7bf77414aef8750d) to send a message via the direct p2p connection.
Awesome, thanks!
Back to top