Brykietosky
Brykietosky

Reputation: 1

Godot supabase realtime connection immediately closes after establishing

I'm new to the Godot engine and trying to connect to my supabase through webSocket. The problem is that after establishing a connection it immediately closes. The application is a game in which I want to check real-time changes in the lobby with a proper ID that I'm getting from an HTTP request when creating a lobby.

Those are my main functions that handle webSocket connection:

func webSocketHandler():
    socket.poll()
    match socket.get_ready_state():
        WebSocketPeer.STATE_OPEN:
            if not is_connected:
                _on_connection_established()
            while socket.get_available_packet_count() > 0:
                var data = socket.get_packet().get_string_from_utf8()
                _on_data_received(data)
        WebSocketPeer.STATE_CLOSED:
            if is_connected:
                _on_connection_closed()
        WebSocketPeer.STATE_CONNECTING:
            print("WebSocket is connecting...")
        WebSocketPeer.STATE_CLOSING:
            print("WebSocket is closing...")


func _on_connection_established():
    is_connected = true
    print("WebSocket connection established.")

    var subscribe_message = {
        "type": "subscribe",
        "topic": "realtime:lobbies.eq" + str(UserData.current_lobby_id),
        "event": "*",
        "ref": "lobby_subscription"
    }
    socket.send_text(JSON.stringify(subscribe_message))
    print("Subscribed to lobbies changes. Current lobby id: " + str(UserData.current_lobby_id))

func _on_data_received(data):
    # Obsługa przychodzących danych
    print("Data received:", data)
    var json = JSON.new()
    var parsed_data = json.parse(data)
    if parsed_data.error != OK:
        print("Failed to parse data:", parsed_data.error_message)
        return

    var message = parsed_data.result
    if message.has("type"):
        match message.type:
            "broadcast":
                _handle_broadcast(message)
            "presence_diff":
                _handle_presence_diff(message)
            _:
                print("Unknown message type:", message.type)
    else:
        print("Received message without a type:", message)

I know that problem propably isnt in supabase url which looks like this: wss://[projectId].supabase.co/realtime/v1/websocket?apikey="+SUPABASE_API_KEY+"&token=" + UserData.token.

Maybe my subscribe_message is wrong? I don't really know how it should be! Any ideas to fix this problem The console looks like this, it is trying to reconnect every 5 sec

Upvotes: 0

Views: 37

Answers (0)

Related Questions