Why Client Relayed Multiplayer is so slow?

i’m hosting on Heroic Cloud and following this: Client Relayed Multiplayer - Heroic Labs Documentation

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:

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()})

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.

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

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.