Metacom Communication Protocol for Metarhia
Metacom protocol specification:
https://github.com/metarhia/Contracts/blob/master/doc/Metacom.md
import { Metacom } from './metacom.js';
const { Metacom } = require('metacom');
const metacom = Metacom.create('https://domainname.com:8000');
(async () => {
const { api } = metacom;
try {
await metacom.load('auth');
await api.auth.status();
} catch (err) {
await api.auth.signIn({ login: 'marcus', password: 'marcus' });
}
await metacom.load('example');
const result = api.example.methodName({ arg1, arg2 });
})();
Streams over Websocket
Example: big file upload
Create uploadFile
function on the client:
const metacom = Metacom.create('https://example.com/api');
const uploadFile = async (file) => {
const uploader = metacom.createBlobUploader(file);
await metacom.api.files.upload({
streamId: uploader.streamId,
name: file.name,
});
await uploader.upload();
return { uploadedFile: file };
};
Create API method to init file destination:
async ({ streamId, name }) => {
const filePath = `./application/resources/${name}`;
const readable = context.client.getStream(streamId);
const writable = node.fs.createWriteStream(filePath);
readable.pipe(writable);
return { result: 'Stream initialized' };
};
Example: big file download
Create downloadFile
function on the client:
const metacom = Metacom.create('https://example.com/api');
const downloadFile = async (name) => {
const { streamId } = await metacom.api.files.download({ name });
const readable = await metacom.getStream(streamId);
const blob = await readable.toBlob();
return new File([blob], name);
};
Create API method to init file source:
async ({ name }) => {
const filePath = `./application/resources/${name}`;
const readable = node.fs.createReadStream(filePath);
const { size } = await node.fsp.stat(filePath);
const writable = context.client.createStream(name, size);
readable.pipe(writable);
return { streamId: writable.streamId };
};
License & Contributors
Copyright (c) 2018-2023 Metarhia contributors.
Metacom is MIT licensed.
Metacom is a part of Metarhia technology stack.