Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@matter/examples
Advanced tools
Matter.js examples and reference implementations
This project provides a set of reference implementations to show several types of Matter device nodes or also a controller implementation.
This package supports all Node.js LTS versions starting with 18.x
To better show how to use the library we provide some reference implementations in the examples directory. They are usable as CLI scripts without changes in the first place but are very simple. They are meant to be used as a starting point for your own implementation, or to experiment with Matter at all in a first place.
For BLE usage please also see the matter-node-ble.js README.md for prerequisites and limitations!
The examples provided by this repository show different use cases demonstrating how to build different device types and show various ways to implement and interact with the matter.js structures. This section gives an overview regarding what the different examples demonstrate.
[!IMPORTANT] With matter.js 0.8 we introduced a new API that is not compatible with the old API. The new API is more flexible and allows for more complex use cases. The old API is still supported but will be removed in a future release. The examples for the old API are available in the NPM package @project-chip/matter-node.js-examples.
Some command line parameters, for example those used to set the level of logging or the MDNS interface are now included in the new environment processing. These can be set by generic command line arguments or by using environment variables. These parameters are processed automatically so are no longer handled by the new example code directly. These are:
--log-level=...
or environment variable MATTER_LOG_LEVEL
or in code environment.vars.set('log.level', Level.DEBUG)
. Allowed values are: Level.FATAL ("fatal"), Level.ERROR ("error"), Level.WARN ("warn"), Level.NOTICE ("notice"), Level.INFO ("info"), Level.DEBUG ("debug")
--log-format=...
or environment variable MATTER_LOG_FORMAT
or environment.vars.set('log.format', Format.PLAIN)
. Allowed values are: Format.PLAIN ("plain"), Format.HTML ("html"), Format.ANSI ("ansi")
--storage-path=...
or environment variable MATTER_STORAGE_PATH
or environment.vars.set('storage.path', "...")
allows to set the storage location. By default, it will be stored in the users home directory in .matter/...
. If specified, the given path will be used relative to the current working directory.
--storage-clear
or environment variable MATTER_STORAGE_CLEAR=1
or environment.vars.set('storage.clear', true)
allows to define if the storage is reset on startup.
--mdns-networkinterface=...
or environment variable MATTER_MDNS_NETWORKINTERFACE
or environment.vars.set('mdns.networkInterface', "...")
allows to limit the DNS announcements and scanning to one network interface. By default, all available interfaces are used.
Additionally, all command line parameters now require to start with two dashes!
BridgedDevicesNode: This example shows how to build a Matter-Bridge that offers multiple OnOff lights and sockets as a bridge. It can be configured via command line and allows for specifications of shell commands to be executed when on and off commands are received at the numbered devices. For more details see below.
ComposedDeviceNode: This example shows how to build a "simple" composed device where multiple OnOff lights and sockets are combined as a single device. The devices are all added at the root level, so no bridge is used. It can be configured via command line and allows for specifications of shell commands to be executed when on and off commands are received at the numbered devices. For more details see below.
DeviceNode: This example shows how to build a simple minimalistic DeviceNode with just one socket or light endpoint. The shell commands to be executed by on/off commands can be configured via CLI.
DeviceNodeFull: This example shows how to build a simple Device node with just one socket or light endpoint. The shell commands to be executed by on/off commands can be configured via CLI. Additionally, this example also shows the following use cases:
MultiDeviceNode: This example shows how to start multiple Matter nodes on one MatterServer where each node is run on its own Port, but share a single MDNS broadcaster and scanner in order to optimize resources. Each node can be configured via CLI to be an onoff socket or a light. CLI. Options also allow specification of shell commands to be executed for on and off commands.
--type humidity
) sensor. The sensor values are updated by default every 60 seconds, or the number of seconds specified via the CLI parameter --interval
. The value can be defined by the output of a CLI script provided via parameter --value
or, if no command is specified, are randomly set in the interval -50..+50. To read the Temperature of a Raspberry Pi as example use --value ="echo \$((\$(</sys/class/thermal/thermal_zone0/temp)/10))"
or to read temperature of your location --value="curl -s 'wttr.in/?format=%t\&m' | sed 's/°C//'| awk '{print \$1*100}'"
(or use ?format=%h
for humidity).Additionally, these two examples are not directly configurable by CLI and mainly show how to implement different combinations of devices:
npm i -g @matter/examples
When you clone the matter.js repository you can also use matter-node.js. To do this you need to execute npm install
in the matter.js root directory once to install all dependencies and build all packages.
Then after cd packages/examples
you can use npm run matter-device
to run the matter-node.js server. Please see the next section for more details.
For production environments where space and/or CPU is at a premium, you might consider using a bundler to distribute your application. This project includes an example demonstrating how to create such a bundle using esbuild.
To run the example, first install as described in From NPM or Use from Cloned Matter.js Repository.
Then in the matter-node.js-examples
installation directory, run:
npm run bundle-device
This creates a single JavaScript file containing the application and dependencies.
To view the example commands for bundling and running, see scripts bundle-device
and matter-device-bundled
in package.json.
Note that you cannot include native dependencies in this way so those will still need to be installed locally using NPM. In this example, those are the dependencies flagged as --external
in the esbuild
command line.
The following CLI parameters are the same for all examples:
The code for this example is in src/examples/DeviceNode.ts.
To run from the build files:
matter-device
To run directly from Typescript files with on the fly compilation:
npm run matter-device
This starts a MatterServer (DeviceNode) server listening on port 5540 and announce it as socket device.
This first version only includes the OnOff cluster (on/off smart thing, like a plug or a bulb). You can use -on and -off parameter to run a script to control something. For instance, on a Raspberry Pi, this will turn on / off the red LED:
matter-device --type socket --on="echo 255 > /sys/class/leds/led1/brightness" --off="echo 0 > /sys/class/leds/led1/brightness"
or when starting from TS files:
npm run matter-device -- --type socket --on="echo 255 > /sys/class/leds/led1/brightness" --off="echo 0 > /sys/class/leds/led1/brightness"
(Please note the "--" to separate commandline parameters between the npm run and the executed script.
If you want to start multiple parallel processes please make sure to use different storage locations, different network port and ideally also different passcodes for each process!
The following parameters are available:
Additionally, there are some Testing parameters:
The code for this example is in src/examples/BridgedDevicesNode.ts.
A Bridge is used to expose multiple devices at once.
The usage and parameter are comparable to above, but the bridge adds support for multiple types and on/off commands:
If you want to start multiple parallel processes please make sure to use different storage locations, different network port and ideally also different passcodes for each process!
The usage is as above but with modified parameters:
Please especially use the --uniqueidX parameters to assign unique own IDs to the single deices in order to remember their structure in the bridge. Such an ID should never be reused if you ever remove or add new devices! If you do not use --uniqueidX then the order you added them here is not allowed to ever change.
matter-bridge --num=2 --on1="echo 255 > /sys/class/leds/led1/brightness" --off1="echo 0 > /sys/class/leds/led1/brightness" --type2=socket --on2="echo 255 > /sys/class/leds/led2/brightness" --off2="echo 0 > /sys/class/leds/led2/brightness"
or when starting from TS files:
npm run matter-bridge -- --num=2 --on1="echo 255 > /sys/class/leds/led1/brightness" --off1="echo 0 > /sys/class/leds/led1/brightness" --type2=socket --on2="echo 255 > /sys/class/leds/led2/brightness" --off2="echo 0 > /sys/class/leds/led2/brightness"
(Please note the "--" to separate commandline parameters between the npm run and the executed script.
The above command exposes two devices under the bridge, one as light, one as socket and executes the respective commands when the devices are turned on or off.
The code for this example is in src/examples/ComposedDeviceNode.ts.
A composed device is one device with multiple different device types combined. This is useful for devices that have multiple functions, e.g. a light bulb with a temperature sensor.
The parameters are like with the bridge but with an added "-type light/socket" parameter to define the type of the composed device itself.
matter-composeddevice --type=socket --num=2 --on1="echo 255 > /sys/class/leds/led1/brightness" --off1="echo 0 > /sys/class/leds/led1/brightness" --type2=socket --on2="echo 255 > /sys/class/leds/led2/brightness" --off2="echo 0 > /sys/class/leds/led2/brightness"
or when starting from TS files:
npm run matter-composeddevice -- --type=socket --num=2 --on1="echo 255 > /sys/class/leds/led1/brightness" --off1="echo 0 > /sys/class/leds/led1/brightness" --type2=socket --on2="echo 255 > /sys/class/leds/led2/brightness" --off2="echo 0 > /sys/class/leds/led2/brightness"
(Please note the "--" to separate commandline parameters between the npm run and the executed script.
The above command exposes a composed device with a socket and a light device and executes the respective commands when the devices are turned on or off.
The code for this example is in src/examples/MultiDeviceNode.ts.
matter.js also allows it to start multiple devices in one process. With this especially the MDNS functionalities are shared between these processes and it should use less resources. How many devices you acn add in one process depends on the load they produce and how many devices run in the single Node.js thread.
The parameters are like with the composed device or bridge.
matter-multidevice --type=socket --num=2 --on1="echo 255 > /sys/class/leds/led1/brightness" --off1="echo 0 > /sys/class/leds/led1/brightness" --type2=socket --on2="echo 255 > /sys/class/leds/led2/brightness" --off2="echo 0 > /sys/class/leds/led2/brightness"
or when starting from TS files:
npm run matter-multidevice -- --type=socket --num=2 --on1="echo 255 > /sys/class/leds/led1/brightness" --off1="echo 0 > /sys/class/leds/led1/brightness" --type2=socket --on2="echo 255 > /sys/class/leds/led2/brightness" --off2="echo 0 > /sys/class/leds/led2/brightness"
(Please note the "--" to separate commandline parameters between the npm run and the executed script.
The above command exposes two single light devices (one socket and on light) and executes the respective commands when the devices are turned on or off.
If you want to start multiple parallel processes please make sure to use different storage locations, different network port and ideally also different passcodes for each process!
The code for this example is in src/examples/ControllerNode.ts.
Experimental The current controller implementation is no CLI tool, but shows the pairing of devices and resuming the connection and also showcase the existing low-level controller API. It is just intended to be used for debugging, during development! The code contains some commented-out examples of how to use the low level controller API. Please do not use this for production, we will replace the API soon!
The controller currently is not discovering the device to pair, but directly connects to the IP/port defined bin the command line parameters.
To run from the build files:
matter-controller -pairingcode 12345678901
To run directly from Typescript files with on the fly compilation:
npm run matter-controller -- -pairingcode 12345678901
This will commission a MatterServer device (for debugging/capability showing purpose only for now).
The following parameters are available and used to initially commission a device (they can be omitted after this):
ot-ctl dataset active -x
to get the value)The code of the examples is written so that you can use them as basis for your own scripts also outside of this library in your own package. If you import from @matter/main
then matter.js loads platform extensions such as those for node.js automatically. If you import directly from our other packages then you should add a dependency on @matter/nodejs
. The examples use @matter/main
imports to ease adoption.
The code is structured by comment blocks to better see what belongs to what. It always has code which is just collecting relevant information and data, then the code which is setting up the device and then the code which is starting the device.
npm run build
: Build all code and create CommonJS and ES6 variants in dist directory. This will built incrementally and only build the changed files.npm run build-clean
: Clean the dist directory and build all code from scratchFAQs
Matter.js examples and reference implementations
The npm package @matter/examples receives a total of 239 weekly downloads. As such, @matter/examples popularity was classified as not popular.
We found that @matter/examples demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.