npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mattermost/client

v10.2.0

Published

JavaScript/TypeScript client for Mattermost

Downloads

9,238

Readme

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.