# P2P Communication

Client-to-client communication or <mark style="color:orange;">peer-to-peer</mark> <mark style="color:orange;">networking</mark> <mark style="color:orange;">(hereinafter referred to as P2P communication)</mark> refers to direct communication between client hosts without going through a server.

This feature is useful in the following situations.

> * Too much data to communicate through the server
> * Clients are geographically close to each other, but the server is too far away
> * If you want less latency by networking directly between clients without going through a server

\
When synchronising movement between layers, the P2P feature can be used to reduce traffic on the server. However, on the flip side, a large number of P2P connections can lead to excessive traffic on the client, and there is always a risk of hacking, so important data such as attack determination should also be checked by the server.

At ProudNet, we manage everything on our servers, including group creation and ungrouping in P2P to make it more hack-proof.

\
To make a P2P connection, the server must first establish a P2P group between clients. Clients can only communicate with clients in the same P2P group as themselves.

Once the server establishes a P2P group between the clients, the clients will communicate via P2P relay through the server, while P2P hole punching between the clients takes place in the background.\
If P2P hole punching is successful, then from then on, communication between clients will be direct, bypassing the server. This is known as hole punching first, relay second.\
\
These features allow ProudNet to send and receive client-to-client messages as soon as a P2P group is formed or changed, and the ProudNet application does not require latency to start a game room. It also reliably performs frequent P2P connections and disconnections for location synchronisation between characters in the line-of-sight in MMORPG games.

If you start client-to-client communication as soon as you form a P2P group, the server will experience a temporary increase in server-side communication for a few seconds.

<figure><img src="/files/TI4WbBx2sJGv8ChQZJPd" alt=""><figcaption><p>Internal progress after P2P communication starts</p></figcaption></figure>

P2P communication between hole-punched clients is done over UDP and ProudNet's proprietary Reliable UDP, but until the hole-punching is successful, P2P relay is done over UDP or TCP. The UDP port used by the client for P2P communication is the same as the UDP port used to connect to the server.

In general, relay communication is more than twice as latent as P2P communication, especially the further apart the servers are in distance.

{% hint style="success" %}
**Reference**

[**Hole Punching**](/proudnet.eng/proudnet-note/dictionary.md#hole-punching)
{% endhint %}

## P2P Group

### - Creating a P2P group

Before you can communicate P2P with other clients on ProudNet, you must create a P2P group on the server. When you create a P2P group on the server with <mark style="color:orange;">Proud.CNetServer.CreateP2PGroup</mark>, clients will receive the event <mark style="color:orange;">Proud.INetClientEvent.OnP2PMemberJoin</mark> for the HostID of clients who are members of the P2P group. After the client receives and processes this, the server receives <mark style="color:orange;">Proud.INetServerEvent.OnP2PGroupJoinMemberAckComplete</mark>.

This event works well when the server wants to do something else after the clients have all detected a change in the P2P group.

<figure><img src="/files/0dQzWqfawLWiMWaLAa4L" alt=""><figcaption><p>Creating a P2P group</p></figcaption></figure>

### - How to use

<figure><img src="/files/OJ2XyJZ4gkLWqktU1tBt" alt=""><figcaption></figcaption></figure>

1\. Create a P2P group by calling the <mark style="color:orange;">CreateP2PGroup</mark> function on the server.\
2\. The client will receive a callback that the P2P group has been created via <mark style="color:orange;">INetClientEvent::OnP2PMemberJoin</mark>.\
3\. The <mark style="color:orange;">OnP2PMemberJoin</mark> function receives the HostID values of the clients and members that joined the group.

<table data-full-width="false"><thead><tr><th width="227">Features</th><th width="105">Callback	</th><th width="190">C++ function</th><th>C# function</th></tr></thead><tbody><tr><td>Adding a new member to a P2P group</td><td>Server</td><td>CNetServer::JoinP2PGroup</td><td>Nettention.Proud.NetServer.JoinP2PGroup</td></tr><tr><td></td><td>Client</td><td>INetClientEvent::OnP2PMemberJoin</td><td>Nettention.Proud.NetClient.P2PMemberJoinHandler</td></tr><tr><td>Removing a new member from a P2P group</td><td>Server</td><td>CNetServer::LeaveP2PGroup</td><td>Nettention.Proud.NetServer.LeaveP2PGroup</td></tr><tr><td></td><td>Client</td><td>INetClientEvent::OnP2PMemberLeave</td><td>Nettention.Proud.NetClient.P2PMemberLeaveHandler</td></tr><tr><td>Removing a P2P group</td><td>Server</td><td>CNetServer::DestroyP2PGroup</td><td>Nettention.Proud.NetServer.DestroyP2PGroup</td></tr><tr><td></td><td>Client</td><td>INetClientEvent::LeaveP2PGroup</td><td>Nettention.Proud.NetClient.LeaveP2PGroup</td></tr></tbody></table>

### - Example usage

**In-game viewgroup handling and chatting**

<table><thead><tr><th width="239">Functions</th><th>Description</th></tr></thead><tbody><tr><td>CreateP2PGroup</td><td>Connecting to a client, creating a chat group</td></tr><tr><td>DestroyP2PGroup</td><td>When a client is disconnected and a chat group is removed</td></tr><tr><td>JoinP2PGroup</td><td>When another user comes into view, when a new user enters the room</td></tr><tr><td>LeaveP2PGroup</td><td>When another user moves out of view, or when an existing user leaves the room</td></tr></tbody></table>

***

## Usage

{% content-ref url="/pages/X8CfVHzjy2qUgXUBhbn3" %}
[Using P2P communication](/proudnet.eng/proudnet/using_pn/p2p/using_p2p.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.proudnet.com/proudnet.eng/proudnet/using_pn/p2p.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
