# Why Client Relayed Multiplayer is so slow?

**URL:** <https://forum.heroiclabs.com/t/why-client-relayed-multiplayer-is-so-slow/5714>\
**Category:** Runtime Framework\
**Created:** [October 17, 2024, 7:38am UTC](https://forum.heroiclabs.com/t/why-client-relayed-multiplayer-is-so-slow/5714 "2024-10-17T07:38:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![axilirate](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/axilirate/32/2336_2.png) [@axilirate](https://forum.heroiclabs.com/u/axilirate)\
**Post date:** [October 17, 2024, 7:38am UTC](https://forum.heroiclabs.com/t/why-client-relayed-multiplayer-is-so-slow/5714/1 "2024-10-17T07:38:00Z")

</div>

i’m hosting on Heroic Cloud and following this: [Client Relayed Multiplayer - Heroic Labs Documentation](https://heroiclabs.com/docs/nakama/concepts/multiplayer/relayed/)

created a simple ping calculation, and the latency is around 500ms (tested with 3 different clients in 3 different locations)

how can i improve it?

how i calculate the ping in godot:

```auto
var ping_history: Array[int] = []
var ping: int

func _ready() -> void:
	Net.received_match_state.connect(_on_match_state)
	

func _on_match_state(state : NakamaRTAPI.MatchData) -> void:
	if not state.op_code == Net.OpCodes.PING_PONG:
		return
	
	if lobby_data.authority == null:
		return
	
	var data: Dictionary = JSON.parse_string(state.data)
	
	if is_match_authority() and data.has("time"):
		Net.socket.send_match_state_async(Net.connected_match.match_id, OpCodes.PING_PONG, JSON.stringify({"time": data["time"]}), [state.presence])
		return
	
	
	ping_history.push_front(Time.get_ticks_msec() - int(data["time"]))
	
	if ping_history.size() > 10:
		ping_history.pop_back()
	
	var total: int = 0
	for p in ping_history:
		total += p
	
	ping = roundi(float(total) / ping_history.size())

func _on_ping_request_timer_timeout() -> void:
	if not is_instance_valid(connected_match):
		return
	
	if is_match_authority():
		return
	
	send_match_state(OpCodes.PING_PONG, {"time": Time.get_ticks_msec()})

```

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [October 22, 2024, 4:29pm UTC](https://forum.heroiclabs.com/t/why-client-relayed-multiplayer-is-so-slow/5714/2 "2024-10-22T16:29:55Z")

</div>

Hello @axilirate,

If I understood the code correctly, you’re measuring:  
A → Nakama → B → Nakama → A

Depending on where you’re located vs the server, and given that this accounts for 4 hops, it’s plausible you’re observing those latencies.

Best.

---

<div class="post-metadata">

**Author:** ![axilirate](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/axilirate/32/2336_2.png) [@axilirate](https://forum.heroiclabs.com/u/axilirate)\
**Post date:** [October 25, 2024, 7:32pm UTC](https://forum.heroiclabs.com/t/why-client-relayed-multiplayer-is-so-slow/5714/3 "2024-10-25T19:32:07Z")

</div>

is there a way to create a direct connection between the players via nakama?

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [October 28, 2024, 11:48am UTC](https://forum.heroiclabs.com/t/why-client-relayed-multiplayer-is-so-slow/5714/4 "2024-10-28T11:48:21Z")

</div>

This would imply a Peer-To-Peer connection which has its own challenges given that clients are usually behind NAT, you should be able to use Nakama as a signalling server but there are no built in capabilities to use it as such.
