Git Product home page Git Product logo

metacom's Introduction

Metacom Communication Protocol for Metarhia

ci status snyk npm version npm downloads/month npm downloads

Metacom protocol specification: https://github.com/metarhia/Contracts/blob/master/doc/Metacom.md

// Load at frontend
import { Metacom } from './metacom.js';

// Load at backend
const { Metacom } = require('metacom');

// Open connection (both platforms) and make calls
const metacom = Metacom.create('https://domainname.com:8000');
(async () => {
  const { api } = metacom;
  try {
    await metacom.load('auth'); // Load `auth` interface
    await api.auth.status(); // Check session status
  } catch (err) {
    await api.auth.signIn({ login: 'marcus', password: 'marcus' });
  }
  await metacom.load('example'); // Load `example` interface
  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) => {
  // createBlobUploader creates streamId and inits file reader for convenience
  const uploader = metacom.createBlobUploader(file);
  // Prepare backend file consumer
  await metacom.api.files.upload({
    streamId: uploader.streamId,
    name: file.name,
  });
  // Start uploading stream and wait for its end
  await uploader.upload();
  return { uploadedFile: file };
};

Create API method to init file destination:

// api/files/upload.js
async ({ streamId, name }) => {
  const filePath = `./application/resources/${name}`;
  // Get incoming stream by streamId sent from client
  const readable = context.client.getStream(streamId);
  // Create nodejs stream to write file on server
  const writable = node.fs.createWriteStream(filePath);
  // Pipe metacom readable to nodejs writable
  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) => {
  // Init backend file producer to get streamId
  const { streamId } = await metacom.api.files.download({ name });
  // Get metacom readable stream
  const readable = await metacom.getStream(streamId);
  // Convert stream to blob to make a file on the client
  const blob = await readable.toBlob();
  return new File([blob], name);
};

Create API method to init file source:

// api/files/download.js
async ({ name }) => {
  const filePath = `./application/resources/${name}`;
  // Create nodejs readable stream to read a file
  const readable = node.fs.createReadStream(filePath);
  // Get file size
  const { size } = await node.fsp.stat(filePath);
  // Create metacom writable stream
  const writable = context.client.createStream(name, size);
  // Pipe nodejs readable to metacom writable
  readable.pipe(writable);
  return { streamId: writable.streamId };
};

License & Contributors

Copyright (c) 2018-2022 Metarhia contributors. Metacom is MIT licensed.
Metacom is a part of Metarhia technology stack.

metacom's People

Contributors

tshemsedinov avatar georgolden avatar o-rumiantsev avatar marhiievhe avatar lundibundi avatar juliagerasymenko avatar rohiievych avatar nechaido avatar vladyslavry141 avatar aliusdiemorietur avatar baristanko avatar zaitsev-oleksii avatar pgnedoy avatar pom4h avatar dependabot[bot] avatar mille-nium avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.