
Security News
US Government Forces Anthropic to Pull Claude Fable Days After Launch
Anthropic says the directive cited national security concerns over a narrow jailbreak, but offered no specific technical details.
matrix-api
Advanced tools
v0.0.0 - early αlphα
Matrix API. 100% frontend.
Please report any and all bugs to the GitHub issue tracker.
Only one install method is required. (npm, cdn, or download).
Install matrix-api with the npm package manager.
This is the recommended install method.
$ npm install matrix-api
import or require the module in your javascript:import { Matrix } from 'matrix-api/Matrix';
or
const Matrix = require('matrix-api/Matrix').Matrix;
If npm isn't an option, matrix-api can be included from a CDN.
You can include both the matrix-api and its dependency, the curvature library with the following script tags:
<script src = "https://unpkg.com/curvature/dist/curvature.js"></script>
<script src = "https://unpkg.com/matrix-api/dist/matrix-api.js"></script>
You can also include only the parts of curvature needed to run matrix api with matrix-api.standalone.js:
<script src = "https://unpkg.com/matrix-api/dist/matrix-api.standalone.js"></script>
Then require the module in your javascript:
const Matrix = require('matrix-api/Matrix').Matrix;
curvature is not required if the standalone build is used.
If you'd prefer to host the files yourself, matrix-api can be included from your own server, or your own CDN.
Both matrix-api.js and matrix-api.standalone.js can be downloaded from the following page:
https://unpkg.com/browse/matrix-api/dist/
Either one of the files can be served by any static HTTP server and used in the browser.
Curvature is available from: https://unpkg.com/browse/curvature/dist/
curvature is not required if the standalone build is used.
Import the scripts from your own server:
<script src = "https://YOUR-SERVER/js/curvature.js"></script>
<script src = "https://YOUR-SERVER/js/matrix-api.js"></script>
or
<script src = "https://YOUR-SERVER/js/matrix-api.standalone.js"></script>
Then require the module in your javascript:
const Matrix = require('matrix-api/Matrix').Matrix;
Set up a page that can serve as your redirect target. When the login flow completes, this is the last page that will load in the popup window.
It will post a message back to its opener when the login flow completes, and then it will close.
// On a page served from /accept-sso
const baseUrl = 'https://matrix.org/_matrix';
const matrix = new Matrix(baseUrl);
// Get the loginToken from the query string.
const query = new URLSearchParams(location.search);
const token = query.get('loginToken');
// Complete the SSO and close the window.
matrix.completeSso(token);
In your main application, initialize a Matrix object.
You can then start the login flow and listen for events:
// In your main application
// Connect to your Matrix endpoint:
const baseUrl = 'https://matrix.org/_matrix';
const matrix = new Matrix(baseUrl);
// Open the login popup, targetting the url from the first step:
const redirectUrl = location.origin + '/accept-sso';
matrix.initSso(redirectUrl);
// ... and wait for the user to log in:
matrix.addEventListener('logged-in', event => {
console.log('Logged in!', event);
// Start polling the server
matrix.listenForServerEvents();
// Act on events of only one type:
matrix.addEventListener('m.room.message', event => console.log('Message:', event));
// Act on events of another type:
matrix.addEventListener('m.reaction', event => console.log('Reaction:', event));
// Act on ALL events from the server:
matrix.addEventListener('matrix-event', event => console.log('Event:', event));
});
Once you're logged in, you can then join a room. A room is a shared event stream that multiple users can read and contribute to.
You'll receive a promise that will complete when the server responds.
const roomId = 'ROOM_ID_HERE';
matrix.joinRoom(roomId)
.then(response => console.log(response))
.catch(error => console.error(error));
The matrix object is an event target, meaning you can listen for events. Listen for the matrix-event type to get all messages:
matrix.addEventListener('matrix-event', event => console.log(event.detail.type, event));
You can also provide an event type to only grab messages of a certain type. Types here map to the second parameter of matrix.putEvent.
matrix.addEventListener('m.room.message', event => console.log(event.detail.type, event));
matrix.addEventListener('app.custom.type', event => console.log(event.detail.type, event));
You can also look for events in the past, optionally within a window of time.
matrix.putEvent.Example:
const sync = matrix.syncRoomHistory(
this.args.roomId
, message => console.log(message)
, Date.now() - (7 * 24 * 60 * 60 * 1000)
, null
, { types: ['message.type.one', 'message.type.two', 'message.type.three'] }
);
Once you've joined one or more rooms, you can send messages to them:
This will also a return promise. It will resolve when the HTTP request completes.
const roomId = 'ROOM_ID_HERE';
const evttype = 'm.room.message';
const msgtype = 'm.text';
const body = 'message body...';
const message = {msgtype, body};
matrix.putEvent(roomId, evttype, message)
.then(response => console.log(response))
.catch(error => console.error(error));
new Matrix(baseUrl, options)Create a new connection to a Matrix server.
false, equivalent to 0.globalThis.sessionStorageconst matrix = new Matrix(baseUrl, {interval, storage});
Starts the 'Single Sign On' login flow with the matrix server. The login flow will open in a popup, allow the user to sign in, and finally. redirect to redirectUri with the token in the loginToken field of the URL's search parameters when complete.
// Connect to your Matrix endpoint:
const baseUrl = 'https://matrix.org/_matrix';
const matrix = new Matrix(baseUrl);
// Open the login popup, targetting the url from the first step:
const redirectUrl = location.origin + '/accept-sso';
matrix.initSso(redirectUrl);
Meant to be called in the popup, on the last page of the SSO login flow, after the matrix server has redirected the user back to the redirectUri passed into matrix.initSso().
Will post a message event back to its own window.opener, targetting the current origin, and automatically close the popup.
const baseUrl = 'https://matrix.org/_matrix';
const matrix = new Matrix(baseUrl);
// Get the loginToken from the query string.
const query = new URLSearchParams(location.search);
const token = query.get('loginToken');
// Complete the SSO and close the window.
matrix.completeSso(token);
Get a login token suitable for guest-level access.
Returns a promise.
matrix.getGuestToken().then(token => console.log(token));
Get the login token associated with the current user's session.
Returns a promise.
matrix.getToken().then(token => console.log(token));
Get the profile for a user, given a userId.
Returns a promise.
matrix.getUserProfile(userId).then(profile => console.log(profile));
Get a chunk of privately accessible data assciated with the current user.
Returns a promise.
const key = 'foobar';
matrix.getUserData(key).then(data => console.log(data));
Store a chunk of privately accessible data assciated with the current user on the server.
Returns a promise.
const key = 'foobar';
const body = 'Hello, World!'
matrix.putUserData(key, body).then(response => console.log(response));
Translate an MXCURL to an HTTP-accessible URL for a given media resource.
Returns a string.
const mediaUrl = matrix.getMediaUrl(mxcUrl);
Download a given media resource and return an ObjectUrl that can be used in HTML.
Returns a promise.
matrix.getMedia(mxcUrl).then(objectUrl => {
const img = document.createElement('img');
img.src = objectUrl;
document.body.appendChild(img);
});
Upload a given media resource to the server.
file input or a drag-n-drop event.https://developer.mozilla.org/en-US/docs/Web/API/File
Returns a promise.
matrix.postMedia(mxcUrl).then(response => console.log(response));
Send a message to a room.
Returns a promise.
matrix.putEvent(roomId, type, body).then(response => console.log(response));
Request message from a channel.
Returns a promise.
matrix.getEvent(roomId, eventId).then(response => console.log(response));
Get the current state configuration of a room.
Returns a promise.
matrix.getRoomState(roomId).then(response => console.log(response));
Get the id of the current user.
Returns a string or boolean false.
const userId = matrix.getUserId();
Create a new room on the server.
Returns a promise.
matrix.createRoom(name,topic,visibility,initialState)
.then(response => console.log(response));
Join a room on the server.
Returns a promise.
matrix.joinRoom(roomId).then(response => console.log(response));
Leave a room on the server.
Returns a promise.
matrix.leaveRoom(roomId).then(response => console.log(response));
Gets the user id for the current logged in user.
Returns a promise.
matrix.whoAmI(roomId).then(response => console.log(response));
Poll the server for new events. Does not return a value. Use addEventListener to capture events.
matrix.listenForServerEvents();
matrix.addEventListener('matrix-event', event => console.log('Event:', event));
Sync the event history of a given room, starting from a given chunk id and moving backward.
{"types": ["m.room.message"]} or for custom types: {"types": ["message.type.one", "message.type.two"]}Internal Method
Internal Method
Normal chat messages must be formatted as such when sending to the server:
const roomId = 'ROOM_ID_HERE';
const body = {
msgtype: 'm.text'
, body: 'THIS IS A NORMAL CHAT MESSAGE.'
};
matrix.putEvent(roomId, 'm.room.message', body);
Reactions to messages is a little more complicated than sending them:
const roomId = 'ROOM_ID_HERE';
const eventId = 'EVENT_ID_HERE';
const body = {
'm.relates_to': {
key: '🍁'
, rel_type: 'm.annotation'
, event_id: eventId
}
};
matrix.putEvent(roomId, 'm.reaction', body);
Matrix transmits events over standard HTTP requests. Meaning polling the server does just that, it opens an HTTP GET to the server and waits for an event to come in. When it does, its transmitted and the connection is closed.
The next connection is then immediately opened and the process repeated. If the connection is timed out and no events are received, the cycle is again immediately repeated.
This may look strange in your network inspector, but rest assured, it is expected behavior.
Perhaps Matrix would do well to implement a simple EventSouce endpoint:
https://developer.mozilla.org/en-US/docs/Web/API/EventSource
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events
Matrix.org serves 10 events per chunk. For large sync operations, this can take some time. I see no way as of yet to optimize this.
Recommened behavior is to cache the events in an IndexedDB and store a highwater and lowwater mark. Highwater is the latest cached chunk, lowwater is the earliest.
Interupted syncs should resume from the lowwater mark since Matrix provides event records in reverse chronological order, and continue to the room's creation date.
A parallel sync can be started from the present momemnt and move backward, ending at the highwater mark.
Once each sync reaches its mark, the current store should have the 100% of the available event history of the given room.
You'll need a copy of NodeJS and NPM.
Files in dist/ are built with brunch.
Changes must be licensed under the Apache-2.0 or more permissive license to be accepted.
Do one thing, do it well.
Tabs, not spaces.
seanmorris/matrix-api
Copyright 2021 Sean Morris
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
FAQs
Matrix API library for the frontend.
The npm package matrix-api receives a total of 69 weekly downloads. As such, matrix-api popularity was classified as not popular.
We found that matrix-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Anthropic says the directive cited national security concerns over a narrow jailbreak, but offered no specific technical details.

Security News
A network of 152 Chrome live wallpaper extensions hid ad tracking and made extension-driven traffic look like Google search clicks.

Company News
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.