{"record":{"id":"07492988de5edd71","repo":"dotnet/aspnetcore","slug":"the-hubconnection-must-be-in-the-disconnected-or-r","errorCode":null,"errorMessage":"The HubConnection must be in the Disconnected or Reconnecting state to change the url.","messagePattern":"The HubConnection must be in the Disconnected or Reconnecting state to change the url\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr/src/HubConnection.ts","lineNumber":188,"sourceCode":"     *  in the disconnected state or if the negotiation step was skipped.\n     */\n    get connectionId(): string | null {\n        return this.connection ? (this.connection.connectionId || null) : null;\n    }\n\n    /** Indicates the url of the {@link HubConnection} to the server. */\n    get baseUrl(): string {\n        return this.connection.baseUrl || \"\";\n    }\n\n    /**\n     * Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or\n     * Reconnecting states.\n     * @param {string} url The url to connect to.\n     */\n    set baseUrl(url: string) {\n        if (this._connectionState !== HubConnectionState.Disconnected && this._connectionState !== HubConnectionState.Reconnecting) {\n            throw new Error(\"The HubConnection must be in the Disconnected or Reconnecting state to change the url.\");\n        }\n\n        if (!url) {\n            throw new Error(\"The HubConnection url must be a valid url.\");\n        }\n\n        this.connection.baseUrl = url;\n    }\n\n    /** Starts the connection.\n     *\n     * @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.\n     */\n    public start(): Promise<void> {\n        this._startPromise = this._startWithStateTransitions();\n        return this._startPromise;\n    }\n","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/ts/signalr/src/HubConnection.ts#L170-L206","documentation":"Thrown by the `baseUrl` setter on HubConnection when the caller tries to reassign the URL while the connection is in any state other than Disconnected or Reconnecting. The library only permits a URL change when no live transport is established, because changing the URL of an active socket/SSE/poll would invalidate the session.","triggerScenarios":"Calling `hubConnection.baseUrl = newUrl` (or the equivalent set) while `_connectionState` is `Connected` or `Disconnecting` — i.e. after start() has resolved and before stop() has completed.","commonSituations":"Redirecting a live connection to a failover/DR endpoint after a reconnect event without first stopping, or attempting to repoint the URL from inside an `on('method')` handler while the connection is up.","solutions":["Stop the connection first (`await hubConnection.stop()`), then set the new URL, then `start()` again.","To redirect during recovery, set the URL from the `onreconnecting` callback (Reconnecting state is permitted).","Do not mutate baseUrl from inside invocation handlers while Connected."],"exampleFix":"// before\nhubConnection.baseUrl = \"https://failover/hub\"; // throws if Connected\n// after - route through reconnect or stop first\nawait hubConnection.stop();\nhubConnection.baseUrl = \"https://failover/hub\";\nawait hubConnection.start();","handlingStrategy":"validation","validationCode":"import { HubConnectionState } from \"@microsoft/signalr\";\nfunction canChangeUrl(hub: { state: HubConnectionState }): boolean {\n  return hub.state === HubConnectionState.Disconnected\n      || hub.state === HubConnectionState.Reconnecting;\n}","typeGuard":"function isUrlChangeAllowed(state: HubConnectionState): boolean {\n  return state === HubConnectionState.Disconnected\n      || state === HubConnectionState.Reconnecting;\n}","tryCatchPattern":null,"preventionTips":["Stop the connection before reassigning baseUrl.","Reassign from onreconnecting if you need mid-failure redirection.","Do not mutate baseUrl from inside invocation handlers."],"tags":["state-machine","url","connection-lifecycle"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}