mattermost/webapp/platform/client
Jesse Hallam 70a50edcf2
[MM-67021] Fix 500 errors on check-cws-connection in non-Cloud environments (#34786)
* Fix 500 errors on check-cws-connection in non-Cloud environments

The check-cws-connection endpoint was returning 500 errors in
self-hosted enterprise environments because:

1. The client only checked BuildEnterpriseReady before making the
   request, which is true for all enterprise builds
2. The server handler didn't check for a Cloud license before
   attempting to connect to CWS
3. The CWS URL is not configured in non-Cloud environments, causing
   the connection check to fail

This fix:
- Server: Add IsCloud() license check to match other cloud endpoints,
  returning 403 instead of 500 for non-Cloud licenses
- Client: Add Cloud license check to skip the request entirely in
  non-Cloud environments

* Add unit tests for check-cws-connection license check

* Return JSON status from check-cws-connection endpoint

Change the check-cws-connection endpoint to return 200 with a JSON body
containing status (available/unavailable) instead of using HTTP error
codes. This allows the endpoint to be used for air-gap detection on
self-hosted instances, not just Cloud deployments.

* i18n

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
2026-02-02 13:41:14 +00:00
..
src [MM-67021] Fix 500 errors on check-cws-connection in non-Cloud environments (#34786) 2026-02-02 13:41:14 +00:00
.eslintrc.json MM-52624/MM-57094 Update ESLint and our ESLint plugin (#26398) 2024-03-13 22:07:28 +00:00
babel.config.js Mono repo -> Master (#22553) 2023-03-22 17:22:27 -04:00
CLAUDE.OPTIONAL.md Add optional Claude.md orchestration for Webapp folder (#34668) 2026-01-14 13:04:20 -05:00
jest.config.js Add shard and log heap usage in Jest (#34656) 2025-12-11 10:54:12 +08:00
package.json Update web app package versions to 11.4.0 (#35003) 2026-01-20 11:58:20 -05:00
README.md Fix calling wrong method in README for ws (#26631) 2024-04-18 14:23:59 -04:00
setup_jest.ts Update to Jest 30 (#34011) 2025-11-12 18:05:50 +00:00
tsconfig.build.json MM-66972 Upgrade to node 24 and main dependencies with babel, webpack and jest (#34760) 2026-01-14 13:14:01 +08:00
tsconfig.json MM-66972 Upgrade to node 24 and main dependencies with babel, webpack and jest (#34760) 2026-01-14 13:14:01 +08:00

Mattermost Client

npm version

This package contains the JavaScript/TypeScript client for Mattermost. It's used by the Mattermost web app and related projects.

Installation

JavaScript

$ npm install @mattermost/client

TypeScript

$ npm install @mattermost/client @mattermost/types

Usage

Rest Client

To use this client, create an instance of Client4, set the server URL, and log in, and then you can start making requests.

import {Client4} from '@mattermost/client';

const client = new Client4();
client.setUrl('https://mymattermostserver.example.com');

client.login('username', 'password').then((user) => {
    // ...
});

If you already have a session token or a user access token, you can call Client4.setToken instead of logging in.

import {Client4} from '@mattermost/client';

const client = new Client4();
client.setUrl('https://mymattermostserver.example.com');

client.setToken('accesstoken');

If needed, methods exist to set other headers such as the User-Agent (Client4.setUserAgent), the CSRF token (Client4.setCSRF), or any extra headers you wish to include (Client4.setHeader).

Methods of Client4 which make requests to the server return a Promise which does the following:

  • On success, the promise resolves to a ClientResponse<T> object which contains the the Response (response), a Map of headers (headers), and the data sent from the server (data).
  • On an error, the promise rejects with a ClientError which contains the error message and the URL being requested. If the error happened on the server, the status code and an error ID (server_error_id) are included.
let user;
try {
    user = (await client.getUser('userid')).data;
} catch (e) {
    console.error(`An error occurred when making a request to ${e.url}: ${e.message}`);
}

WebSocket Client

To use the WebSocket client, create an instance of WebSocketClient and then call its initialize method with the connection URL and an optional session token or user access token. After that, you can call the client's addMessageListener method to register a listener which will be called whenever a WebSocket message is received from the server.

import {WebSocketClient} from '@mattermost/client';

// If you already have an instance of Client4, you can call its getWebSocketUrl method to get this URL
const connectionUrl = 'https://mymattermostserver.example.com/api/v4/websocket';

// In a browser, the token may be passed automatically from a cookie
const authToken = process.env.TOKEN;

const wsClient = new WebSocketClient();
wsClient.initialize(connectionUrl, authToken);

wsClient.addMessageListener((msg) => {
    if (msg.event === 'posted') {
        console.log('New post received', JSON.parse(msg.data.post));
    }
});

Node.js

Note that WebSocketClient expects globalThis.WebSocket to be defined as it was originally written for use in the Mattermost web app. If you're using it in a Node.js environment, you should set globalThis.WebSocket before instantiating the WebSocketClient.

import WebSocket from 'ws';

if (!globalThis.WebSocket) {
    globalThis.WebSocket = WebSocket;
}

const wsClient = new WebSocketClient();

This can also be done using dynamic imports if you're using them.

if (!globalThis.WebSocket) {
    const {WebSocket} = await import('ws');
    globalThis.WebSocket = WebSocket;
}

const wsClient = new WebSocketClient();

Compilation and Packaging

As a member of Mattermost with write access to our NPM organization, you can build and publish this package by running the following commands:

npm run build --workspace=platform/client
npm publish --workspace=platform/client

Make sure to increment the version number in package.json first! You can add -0, -1, etc for pre-release versions.