1.1.4 • Published 22 days ago

linkmeup v1.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
22 days ago

LinkMeUp - the easiest way to link NodeJS microservices with each other

Currently the project is at the "Working prototype" stage.

Features

  • Calling remote methods

  • Transfer the buffer in binary

  • Calling long methods with feedback functions

  • Calling methods with stream arguments

Install

yarn install linkmeup

How to use it

First, you need two projects - root service project and microservice project. A project with a microservice will be called a server because it accepts requests from the root.

In microservice project create server instance, like

import { createServer } from 'linkmeup'
const server = new createServer("imageConverter")

Then, add some async method for your server and launch the server, like:

/** Convert image to another image */
server.addMethod("processImage", async (image: Buffer) => {
  const result = await processImage(image)
  return result
})

...

server.listen(7800, 'localhost')

In the second step, we need to build a file with types to send to the client. You can do this using the command line, and then modify it if necessary:

yarn linkmeup generate

This will result in the file linkmeup.d.ts with the following content:

declare module "linkmeup" {
    interface LinkMeUpClients {
        imageConverter: {
            /** Convert image to another image */
            processImage(image: Buffer): Promise<Buffer>;
        };
    }
}
export {}

This file needs to be placed in the client to pull up the right types. Usually, it is enough to place it anywhere for the typescript to pick it up.

That's it, now all that's left is to connect to our server:

import { createClient } from 'linkmeup'

const client = createClient("imageConverter", "http://localhost:7800")

The methods are called as usual methods with the names that were assigned on the server:

const resultImage = await client.processImage(image)

How to call long methods

In the previous section, when calling methods, the usual HTTP call is used, which waits for the completion of the function. This is a simple and fast scheme, but your request may drop by timeout (e.g. it will be killed by nginx).

To avoid this, you can use long methods. In this case, a request is sent, which immediately receives a response with the requestId. Then, at a certain interval, the client asks the server about the status of the request. This also allows you to make a callback on the server.

Example: You upload a video to a microservice for compression. The video is long and takes a long time to process, in addition you would like to know what percentage of it has been processed. To do this, you declare a long method and callback every time you update your progress.

Extend last example. We use addLongMethod instead addMethod

server.addLongMethod("processVideo", async (video: Buffer, callback: (progress: number) => void) => {
  const result = await processVideo(video, (progress) => {
    callback(progress)
  })
  return result
})

And that's it! Also generate the d.ts files and move quickly to the client.

const resultVideo = await client.processVideo(video, (progress: number) => {
  console.log(`Progress: ${progress}`)
})

console.log(`Target video: ${resultVideo}`)
1.1.4

22 days ago

1.1.1

1 month ago

1.1.0

1 month ago

1.1.0-b

1 month ago

1.1.0-a

1 month ago

1.1.3

1 month ago

1.1.2

1 month ago

1.0.14

2 months ago

1.0.11

3 months ago

1.0.13

3 months ago

1.0.12

3 months ago

1.0.10

3 months ago

1.0.9

3 months ago

1.0.8

3 months ago

1.0.7

3 months ago

1.0.6

3 months ago

1.0.5

3 months ago

1.0.4

3 months ago

1.0.3

3 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago