Matter.js WebSocket Server

0.6.1 · active · verified Sun Apr 19

The `matter-server` package implements a comprehensive Matter Controller Server over WebSockets, leveraging the JavaScript Matter SDK `matter.js` as its foundational layer. It currently stands at stable version `0.6.1`, with a development cadence that includes frequent nightly alpha releases alongside regular official updates, indicating active and continuous development. This project differentiates itself by providing a full-fledged web server, a WebSocket endpoint that adheres to the Open Home Foundation (OHF) Matter Server API (with logic encapsulated in `@matter-server/ws-controller`), and an interactive dashboard (powered by `@matter-server/dashboard`). It also integrates community-provided custom Matter clusters via `@matter-server/custom-clusters` and ensures backward compatibility with the OHF Python Matter Server, facilitating migration and interoperability for existing users. Client applications can connect using the companion `@matter-server/ws-client` library. The server is designed for modern Node.js environments, specifically requiring Node.js versions `>=20.19.0 <22.0.0` or `>=22.13.0`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes and starts the `matter-server` on a specified port, demonstrating basic setup and graceful shutdown for a WebSocket Matter controller.

import { MatterServer, MatterServerOptions } from 'matter-server';
import { Logger } from '@project-chip/matter-node.js/log';

const MATTER_PORT = parseInt(process.env.MATTER_PORT ?? '5580', 10);
const MATTER_SERVER_ADDRESS = process.env.MATTER_SERVER_ADDRESS ?? '0.0.0.0';
const MATTER_FRONTEND_PATH = process.env.MATTER_FRONTEND_PATH ?? undefined;

async function startMatterServer() {
  const logger = new Logger('MatterServerQuickstart');

  const serverOptions: MatterServerOptions = {
    port: MATTER_PORT,
    host: MATTER_SERVER_ADDRESS,
    frontendPath: MATTER_FRONTEND_PATH,
    // Optionally configure Matter.js backend settings here
    // For example, persistent storage paths, device setup, etc.
  };

  const server = new MatterServer(serverOptions);

  try {
    logger.info(`Starting Matter WebSocket Server on ${MATTER_SERVER_ADDRESS}:${MATTER_PORT}...`);
    await server.start();
    logger.info(`Matter WebSocket Server started successfully.`);
    if (!MATTER_FRONTEND_PATH) {
        logger.info(`Dashboard available at http://localhost:${MATTER_PORT}`);
    }
    logger.info(`WebSocket endpoint: ws://localhost:${MATTER_PORT}`);

    // Keep the server running until explicitly stopped
    process.on('SIGINT', async () => {
      logger.info('Shutting down Matter WebSocket Server...');
      await server.close();
      logger.info('Matter WebSocket Server stopped.');
      process.exit(0);
    });

  } catch (error) {
    logger.error(`Failed to start Matter WebSocket Server: ${error}`);
    process.exit(1);
  }
}

startMatterServer();

view raw JSON →