Registry / web-framework / matrix-widget-api

matrix-widget-api

JSON →
library1.17.0jsnpmunverified

The `matrix-widget-api` package provides a JavaScript/TypeScript SDK for both Matrix widgets and the clients that host them, facilitating secure and standardized communication. It allows widgets to request capabilities (like sending messages or accessing state) and clients to drive widget behavior and respond to these requests. Currently at stable version 1.17.0, the library typically sees minor releases on a monthly or bi-monthly cadence, introducing new features, bug fixes, and sometimes breaking changes due to the evolving nature of the Matrix widget specification (which is not yet fully standardized). Its key differentiator is being the official SDK from Matrix.org, ensuring alignment with their reference implementations, though this also means it may be subject to changes as the underlying specification matures.

npm install matrix-widget-api
INSTALL
IMPORT
SIG · MATRIX-WIDGET-API
M
matrix-widget-api
web-frameworkjavascriptv1.17.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

WidgetApi
import { WidgetApi } from 'matrix-widget-api';
const WidgetApi = require('matrix-widget-api').WidgetApi;
This is the primary class for implementing the widget side of the communication. The package primarily uses ESM imports, though a UMD build is available for direct browser use via script tags.
ClientWidgetApi
import { ClientWidgetApi } from 'matrix-widget-api';
const ClientWidgetApi = require('matrix-widget-api').ClientWidgetApi;
This class is used by Matrix clients (or hosts) to interact with and manage widgets embedded within them.
MatrixCapabilities
import { MatrixCapabilities } from 'matrix-widget-api';
import MatrixCapabilities from 'matrix-widget-api';
This object/enum provides predefined Matrix capabilities that widgets can request from the client. It is a named export, not a default export.
WidgetApiToWidgetAction
import { WidgetApiToWidgetAction } from 'matrix-widget-api';
Enum representing actions sent from the Widget API to the widget, useful for handling custom event listeners.

This quickstart demonstrates how to initialize `WidgetApi` within a Matrix widget, request capabilities, register custom action handlers, start the communication, and send basic messages.

import { WidgetApi, MatrixCapabilities, WidgetApiToWidgetAction } from 'matrix-widget-api'; // In a widget: const widgetId = null; // Supply widget ID if known, otherwise null const api = new WidgetApi(widgetId); // Request necessary capabilities early api.requestCapability(MatrixCapabilities.Screenshots); api.requestCapabilities([MatrixCapabilities.Stickerpicker, MatrixCapabilities.SendEvent]); // Add custom action handlers if your widget needs to respond to specific events api.on(`action:${WidgetApiToWidgetAction.UpdateVisibility}`, (ev) => { ev.preventDefault(); // Indicate that this widget is handling the event console.log("Visibility updated:", ev.detail); // Custom handling logic here api.transport.reply(ev.detail, {}); // Acknowledge receipt }); api.on("action:com.example.my_custom_action", (ev) => { ev.preventDefault(); console.log("Received custom action:", ev.detail); api.transport.reply(ev.detail, { status: "received", processed: true }); }); // Start the communication messaging api.start(); // If your widget doesn't need to wait for an iframe load event, inform the client it's ready // (Important for widgets that initialize quickly) api.sendContentLoaded(); // Later, send messages or update widget state api.setAlwaysOnScreen(true); // Example: request the client to keep the widget always visible api.transport.send("com.example.another_action", { data: "hello from widget" }); console.log("Widget API initialized and started.");
Debug
Known issues
breakingVersion 1.14.0 introduced breaking changes to `WidgetApi` methods for updating delayed events and `WidgetDriver` methods for performing these updates. This affects experimental APIs.
fix
Review the changes in https://github.com/matrix-org/matrix-widget-api/pull/143 and update your `WidgetApi` and `WidgetDriver` implementations to match the new method signatures for delayed events.
affects: >=1.14.0
breakingVersion 1.13.0 introduced the `UPDATE_STATE` widget API version. Clients (like Element Web) will treat widgets claiming compatibility with this version differently, no longer automatically sending state updates via the `send_event` action.
fix
If your widget claims compatibility with `UPDATE_STATE` via the API version, you must implement manual handling for state updates and not rely on automatic `send_event` actions from the client. Adjust your widget's `update_state` logic accordingly.
affects: >=1.13.0
deprecatedVersion 1.12.0 deprecated `WidgetDriver.readRoomEvents`, `WidgetDriver.readRoomState`, and the `currentViewedRoomId` parameter of `ClientWidgetApi.feedEvent`. These APIs should be migrated.
fix
Migrate from `WidgetDriver.readRoomEvents` to `WidgetDriver.readTimelineEvents`. It is recommended to migrate all three deprecated APIs to their new solutions simultaneously for consistency.
affects: >=1.12.0
gotchaThe Matrix Widget API is currently experimental and not yet part of the official Matrix specification. This means the library's behavior, API surface, and compatibility are subject to change, and it may not work with non-Matrix.org implementations.
fix
Stay informed about new releases of `matrix-widget-api` and developments in the Matrix specification. Test your widgets thoroughly with target clients and be prepared for potential breaking changes as the spec evolves.
affects: *
gotchaPrior to v1.17.0, `PostmessageTransport` instances could leak window event listeners when stopped, potentially leading to memory issues or unexpected behavior over time.
fix
Upgrade to `matrix-widget-api` version 1.17.0 or newer to ensure `PostmessageTransport` instances properly clean up their event listeners upon being stopped.
affects: <1.17.0
Errors
Common errors & fixes
Widget not receiving room state updates automatically when claiming UPDATE_STATE API version.
Since v1.13.0, clients no longer send automatic state updates via `send_event` to widgets claiming compatibility with the `UPDATE_STATE` API version.
fix
Implement manual handling for state updates within your widget, as the responsibility for managing state events shifts to the widget when `UPDATE_STATE` is supported.
TypeError: driver.readRoomEvents is not a function (or similar for readRoomState, feedEvent currentViewedRoomId parameter).
The methods `WidgetDriver.readRoomEvents`, `WidgetDriver.readRoomState`, and the `currentViewedRoomId` parameter of `ClientWidgetApi.feedEvent` were deprecated in v1.12.0.
fix
Migrate your client implementation to use `WidgetDriver.readTimelineEvents` and other new solutions introduced in v1.12.0 for reading room events and state. Ensure all three deprecated APIs are updated simultaneously.
Widget functionality is blocked or specific actions fail to execute.
The widget likely requested capabilities that were not granted by the hosting client.
fix
Ensure your widget requests all necessary capabilities (e.g., `MatrixCapabilities.SendEvent`) early in its lifecycle using `api.requestCapability()` or `api.requestCapabilities()`. Verify the client implementation is correctly responding to and granting these requests.
ReferenceError: mxwidgets is not defined (when using script tag in browser).
The `matrix-widget-api` UMD bundle was not loaded correctly, or the global `mxwidgets` object is being accessed before the script has fully executed.
fix
Check the `src` attribute of your `<script>` tag to ensure the correct `unpkg.com` path and version (e.g., `https://unpkg.com/matrix-widget-api@1.17.0/dist/api.min.js`). Ensure your code accessing `mxwidgets` runs after the script has loaded, typically within a `DOMContentLoaded` listener.
Upgrade
Version history
1.17.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
4
Resources