3.2.3 • Published 7 days ago

metacom v3.2.3

Weekly downloads
254
License
MIT
Repository
github
Last release
7 days ago

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

import { Metacom } from 'metacom';
// const { Metacom } = require('metacom'); // for backend

const metacom = Metacom.create('ws://domainname.com:8000');
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('ws://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('ws://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-2024 Metarhia contributors. Metacom is MIT licensed.\ Metacom is a part of Metarhia technology stack.

3.2.3

7 days ago

3.2.1

3 months ago

3.2.0

5 months ago

3.1.2

6 months ago

3.1.1

7 months ago

3.1.0

7 months ago

3.0.6

8 months ago

3.0.5

8 months ago

3.0.0-alpha.13

11 months ago

3.0.0-alpha.10

1 year ago

3.0.0-alpha.12

11 months ago

3.0.0-alpha.11

11 months ago

3.0.4

9 months ago

3.0.3

9 months ago

3.0.2

9 months ago

3.0.1

10 months ago

3.0.0

10 months ago

3.0.0-alpha.9

1 year ago

3.0.0-alpha.7

1 year ago

3.0.0-alpha.6

1 year ago

3.0.0-alpha.8

1 year ago

3.0.0-alpha.5

1 year ago

3.0.0-alpha.1

2 years ago

3.0.0-alpha.3

2 years ago

3.0.0-alpha.2

2 years ago

3.0.0-alpha.4

2 years ago

2.0.8

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.5

2 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.5

3 years ago

1.7.4

3 years ago

1.7.3

3 years ago

1.7.2

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.3

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

1.0.0-alpha.0

3 years ago

0.1.0-alpha.11

3 years ago

0.1.0-alpha.10

3 years ago

0.1.0-alpha.9

4 years ago

0.1.0-alpha.8

4 years ago

0.1.0-alpha.7

4 years ago

0.1.0-alpha.6

4 years ago

0.1.0-alpha.5

4 years ago

0.1.0-alpha.4

4 years ago

0.1.0-alpha.3

4 years ago

0.1.0-alpha.2

4 years ago

0.1.0-alpha.1

4 years ago

0.1.0-alpha.0

4 years ago

0.0.0

6 years ago