Reconnecting to authoritative server match after disconnect best practice

Hello there! At the moment I’m writing a feature to handle re-connections/re-authentications if a player temporarily loses network access during a session. I mostly wanted to sanity check that this is the best approach (using the Godot 4 client).

extends Node

var _match_data
var _attempt_reconnect = false
var _reconnection_timer = 0
var client: NakamaClient
var session: NakamaSession
var socket: NakamaSocket
var _match: NakamaRTAPI.Match

# Called when the node enters the scene tree for the first time.
func _ready():
	var host = "<>"
	client = Nakama.create_client("defaultkey", host, 7350, "http")
	await _do_attempt_connection()
func _process(delta):
		_reconnection_timer += delta
		# attempt reconnection every 3 seconds
		if _reconnection_timer > 3:
			_reconnection_timer = 0
			_attempt_reconnect = false;
func find_match():
	var result = await socket.rpc_async("FindMatchRpc")
	_match_data = result.payload
	_match = await socket.join_match_async(_match_data)
	return result
func _on_socket_connected():
	_attempt_reconnect = false

func _on_socket_closed():
	# connection lost
	_attempt_reconnect = true
	socket = null
	# reattempt connection

func _do_attempt_connection():
	if session == null || session.expired:
		# Get the System's unique device identifier
		var device_id = OS.get_unique_id()
		session = await client.authenticate_device_async(device_id)
	if socket == null || !socket.is_connected_to_host():
		socket = Nakama.create_socket_from(client)
		var conn = await socket.connect_async(session)
		if !socket.is_connected_to_host():
			socket = null
			_attempt_reconnect = true
			# rejoin a match if one exists
			if _match_data != null:
				_match = await socket.join_match_async(_match_data)
		_attempt_reconnect = false

Hi, @tchom!

For reconnection strategy I recommend you use the closed socket signal.
You can find the list of all signals here:
And some examples on how to use it here:

Also, note the socket can be set like describe on Heroic Labs’ doc: Heroic Labs Documentation | Godot 4

Hope this helps.