# room\_broadcast

## room\_broadcast Sample

### Overview

The room\_broadcast sample is a demonstration program utilizing the Room and P2P features of the Diarkis server. It allows two users to connect to the same Room, conduct relay communication via the Room, then establish a P2P connection for direct communication.\
The room\_broadcast sample showcases the following features:

* Room creation
* Joining a Room
* Sending messages to Room members
* Initiating P2P
* Sending and receiving messages via P2P

### Starting the Server in Your Local Environment

Execute the tutorial to start the server used in the sample and launch the Diarkis server in your local environment.

[1. Launch Diarkis Server in Local Environment](/en/getting-started/tutorial/setup-local-server.md)

## Explanation of the room\_broadcast Sample

1. Initialize the Diarkis runtime and Diarkis Module and connect to the Diarkis server.\
   For more details, refer to [Overall Flow for Using Diarkis Module](/en/diarkis-client/diarkis-module.md#diarkis-module-usage-overview).
2. Initialize the necessary functions. Since this sample uses the Room and P2P modules, initialize those functions using the Diarkis Module.

   ```
   // diarkis =  Diarkis::DiarkisAllocShared<DiarkisInterface>(uid); 
   ...
   // Setup P2P module
   diarkis->SetupP2P();

   // Setup Room module
   diarkis->SetupRoom(false);

   ```
3. Join a Room using RandomJoin. `DiarkisRoomBase::RandomJoinRoom()` will join an existing Room if available, or create a new one if not. After sending a request to the server via `DiarkisRoomBase::RandomJoinRoom()`, check the status with `DiarkisRoomBase::IsJoin()` until the Room join is complete.

   ```cpp
   diarkis->RandomJoinRoom(10, 60, 0, true);

   // Wait for DiarkisRoomBase::OnRoomJoin
   while (diarkis->GetRoomBase()->IsJoin() == false) {
       std::this_thread::sleep_for(std::chrono::milliseconds(30));
   }
   ```
4. Wait until the expected number of participants join the Room.\
   In this sample, wait until 2 users join the same Room. You can request the list of members in the Room by calling `DiarkisRoomBase::SendGetMemberIDs()`. The results can be obtained with `DiarkisRoomBase::GetRoomMembers()`.

   ```cpp
   while (1)
   {
       Diarkis::StdVector<Diarkis::StdString> members;

       diarkis->GetRoomBase()->SendGetMemberIDs();
       diarkis->GetRoomBase()->GetRoomMembers(members);

       if (members.size() == NumPeer)
       {
           break;
       }
       std::this_thread::sleep_for(std::chrono::milliseconds(2000));
   }
   ```
5. Use Room Broadcast to send a message to all users in the Room.

   ```cpp
   diarkis->GetRoomBase()->SendBroadcastToRoom(buff, bReliable);
   ```
6. The Room owner executes `DiarkisRoomBase::SendStartP2PSync()` to notify the server of the initiation of P2P connections and to retrieve a list of address connections for participants in the Room.

   ```cpp
   if (Diarkis::StdString(uid.c_str()) == diarkis->GetRoomBase()->GetOwnerUID())
   {
       // Retrieve address list of each member's connection
       diarkis->GetRoomBase()->SendStartP2PSync();
   }
   ```
7. When the Room owner executes `DiarkisRoomBase::SendStartP2PSync()`, P2P connection information is notified to all users in the Room via `DiarkisRoomBase::OnStartP2PSync()`, triggering the start of hole punching.
8. Upon successful hole punching, communication via P2P begins.

   ```cpp
   diarkis->GetP2PBase()->SendBroadcast(buff, RudpType::UNRELIABLE_UNORDERED);
   ```
9. Execute `DiarkisRoomBase::SendLeaveRoom()` to request to leave the Room from the server. In this sample, wait until leaving the Room is complete using `DiarkisRoomBase::IsLeave()`.

   ```cpp
   // Leave the Room
   diarkis->SendLeaveRoom();

   // Wait for DiarkisRoomBase::OnRoomLeave
   while (diarkis->GetRoomBase()->IsLeave() == false) {
       std::this_thread::sleep_for(std::chrono::milliseconds(30));
   }
   ```
10. Termination\
    For more details, refer to [Overall Flow for Using Diarkis Module](/en/diarkis-client/diarkis-module.md#diarkis-module-usage-overview).

## Notes

* To successfully achieve P2P hole punching, the clients must be in the same Room and exchange addresses.
* Please prepare multiple clients and execute them simultaneously to verify operation.


---

# 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://help.diarkis.io/en/diarkis-modules/room/sample/room-broadcast.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.
