Peer-to-Peer Communication With Diarkis

Diarkis allows you to implement P2P (peer-to-peer) between the client devices. The server cluster of Diarkis acts as both STUN servers and TURN servers in order for the client devices to communicate directly.
Because P2P allows devices to communicate directly, the latency is usually kept to be very minimum (You must be aware of physical distance between the client devices more, though). It is suitable for latency-sensitive applications. It also moves the workload from the servers to the client devices, so the server workload will be kept minimum while client devices’ stress may increase.

What Is Peer-to-Peer?

Peer-to-peer or P2P is a form of distributed network architecture. Instead of having a central server(s) to relay messages between devices, P2P allows the devices to send and receive messages directly.
In this article, we will focus only on the direct communications between devices as it is also the focus of Diarkis’ P2P. If you are interested in P2P networks and mesh networks, here is a relevant article on the topic.

P2P Basics – STUN Servers

STUN Servers stands for Session Traversal Utilities for NAT. It is a mouthful, I know. It basically means STUN servers serve as an intermediary discovery service for devices that wish to communicate directly.
Devices send a UDP packet to STUN servers and have the STUN servers discover their public addresses so that the devices may receive messages at these addresses. Usually, devices sit behind routers and the locally visible addresses and publically visible addresses are different. In order for devices to find their public addresses, they need STUN servers: STUN servers look at each UDP packet header from devices to discover the source address (NAT Traversal). If you wish to learn more details on STUN servers, this article might be a good one to read.

P2P Basics – TURN Servers

TURN stands for Traversal Using Relays around NAT. The technique used by STUN servers called NAT traversal may fail. It is affected heavily by the device’s network environment (routers etc.).
There are other causes for P2P communications to fail also. When deveices fail to communicate directly, they send messages to TURN servers to “relay” the messages to other devices, so that the devices may still exchange messages. More details on TURN servers maybe found here.

P2P Basics – Hole Punching

Once devices exchange each other’s public addresses through STUN servers, these devices must send packets to each other. Routers are usually configured to drop packets received from unknow sources. By sending packets to other devices, they will become “known” to the router and will be able to receive message packets from those “known” devices. This is called Hole punching. Diarkis’ client SDK for P2P handles this internally when Connect method is called.

How Does Diarkis Enables P2P?

Diarkis’ distributed arhitecture of the server cluster serves as both STUN and TURN servers. Devices may initiate P2P communication through Diarkis’ client SDK and fallback to TURN servers if necessary.
Diarkis client SDK provides P2P interface. For C# client SDK, read here. Diarkis’ built-in modules also helps you to build applications using P2P.
For example, Match Making module will allow your client devices to search other client devices based on custom attributes. And Room module and/or Field module will make the address exchange between the client deveices simple.
Room module will help you relay messages to other client devices as TURN server would should you have to fallback to relay method.

Value Of Using Diarkis P2P

First of all, scaling out STUN and TURN servers is not easy because both servers are stateful servers. It means that STUN and TURN servers must manage and maintan the connected device states.
You cannot simply add servers to handle more server traffic without providing some form of load balancing.
You cannot simply remove servers to scale in because the states of the devices stored on the removed servers will be gone and STUN and TURN would not perform as expected.
Diarkis’ server cluster solve both problems. The horizontally scalable design of Diarkis server cluster allows scaling out and scaling in without providing anything extra. It can manages the connected devices’ states accordingly.

C# Client Code Example

Below is an example of C# client code showcasing how to implement P2P using Diarkis C# client SDK:
can manages the connected devices’ states accordingly.

C# Client Code Example

Below is an example of C# client code showcasing how to implement P2P using Diarkis C# client SDK:

Contact us

For inquiries about Diarkis introduction, employment entry, etc., click here.