diff --git a/app/client/websocket/index.ts b/app/client/websocket/index.ts index 6b9ddb34ea..f265c1ce2b 100644 --- a/app/client/websocket/index.ts +++ b/app/client/websocket/index.ts @@ -6,7 +6,7 @@ import {Platform} from 'react-native'; import {WebsocketEvents} from '@constants'; import DatabaseManager from '@database/manager'; -import {getConfig} from '@queries/servers/system'; +import {getConfigValue} from '@queries/servers/system'; import {hasReliableWebsocket} from '@utils/config'; import {toMilliseconds} from '@utils/datetime'; import {logError, logInfo, logWarning} from '@utils/log'; @@ -79,8 +79,12 @@ export default class WebSocketClient { return; } - const config = await getConfig(database); - const connectionUrl = (config.WebsocketURL || this.serverUrl) + '/api/v4/websocket'; + const [websocketUrl, version, reliableWebsocketConfig] = await Promise.all([ + getConfigValue(database, 'WebsocketURL'), + getConfigValue(database, 'Version'), + getConfigValue(database, 'EnableReliableWebSockets'), + ]); + const connectionUrl = (websocketUrl || this.serverUrl) + '/api/v4/websocket'; if (this.connectingCallback) { this.connectingCallback(); @@ -101,7 +105,7 @@ export default class WebSocketClient { this.url = connectionUrl; - const reliableWebSockets = hasReliableWebsocket(config); + const reliableWebSockets = hasReliableWebsocket(version, reliableWebsocketConfig); if (reliableWebSockets) { // Add connection id, and last_sequence_number to the query param. // We cannot also send it as part of the auth_challenge, because the session cookie is already sent with the request. @@ -129,6 +133,11 @@ export default class WebSocketClient { headers.Authorization = `Bearer ${this.token}`; } const {client} = await getOrCreateWebSocketClient(this.url, {headers, timeoutInterval: WEBSOCKET_TIMEOUT}); + + // Check again if the client is the same, to avoid race conditions + if (this.conn === client) { + return; + } this.conn = client; } catch (error) { return; diff --git a/app/utils/config.ts b/app/utils/config.ts index ed9aacfff9..8029073e2d 100644 --- a/app/utils/config.ts +++ b/app/utils/config.ts @@ -3,10 +3,10 @@ import {isMinimumServerVersion} from './helpers'; -export function hasReliableWebsocket(config: ClientConfig) { - if (isMinimumServerVersion(config.Version, 6, 5)) { +export function hasReliableWebsocket(version?: string, reliableWebsocketsConfig?: string) { + if (version && isMinimumServerVersion(version, 6, 5)) { return true; } - return config.EnableReliableWebSockets === 'true'; + return reliableWebsocketsConfig === 'true'; } diff --git a/package-lock.json b/package-lock.json index 49113024ae..e5fb3b60f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,7 @@ "@gorhom/bottom-sheet": "4.4.5", "@mattermost/compass-icons": "0.1.35", "@mattermost/react-native-emm": "1.3.5", - "@mattermost/react-native-network-client": "1.3.1", + "@mattermost/react-native-network-client": "1.3.2", "@mattermost/react-native-paste-input": "0.6.2", "@mattermost/react-native-turbo-log": "0.2.3", "@mattermost/react-native-turbo-mailer": "0.2.4", @@ -3241,9 +3241,9 @@ } }, "node_modules/@mattermost/react-native-network-client": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/@mattermost/react-native-network-client/-/react-native-network-client-1.3.1.tgz", - "integrity": "sha512-DtwVLV/NUE6MkXOlVZG+4QJXou6nHMdmsxnP1+RqhOeSw5jJlQvxmQgxzxvxLpaWOag+wgB1zpDulGNbr/Cz6Q==", + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@mattermost/react-native-network-client/-/react-native-network-client-1.3.2.tgz", + "integrity": "sha512-3GFNzMXZWlIXXDYQLIJlKRf+HUZKP0F7mpZ1rSTgoTmUeFdqde4uRiU/L96COg34rAdeFRFrgpk0DxEnT7NiVg==", "dependencies": { "validator": "13.9.0", "zod": "3.20.6" @@ -24297,9 +24297,9 @@ "requires": {} }, "@mattermost/react-native-network-client": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/@mattermost/react-native-network-client/-/react-native-network-client-1.3.1.tgz", - "integrity": "sha512-DtwVLV/NUE6MkXOlVZG+4QJXou6nHMdmsxnP1+RqhOeSw5jJlQvxmQgxzxvxLpaWOag+wgB1zpDulGNbr/Cz6Q==", + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@mattermost/react-native-network-client/-/react-native-network-client-1.3.2.tgz", + "integrity": "sha512-3GFNzMXZWlIXXDYQLIJlKRf+HUZKP0F7mpZ1rSTgoTmUeFdqde4uRiU/L96COg34rAdeFRFrgpk0DxEnT7NiVg==", "requires": { "validator": "13.9.0", "zod": "3.20.6" diff --git a/package.json b/package.json index 4ca4715fd3..7b440471aa 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "@gorhom/bottom-sheet": "4.4.5", "@mattermost/compass-icons": "0.1.35", "@mattermost/react-native-emm": "1.3.5", - "@mattermost/react-native-network-client": "1.3.1", + "@mattermost/react-native-network-client": "1.3.2", "@mattermost/react-native-paste-input": "0.6.2", "@mattermost/react-native-turbo-log": "0.2.3", "@mattermost/react-native-turbo-mailer": "0.2.4",