nanoporetech / minknow_api

Protobuf and gRPC specifications for the MinKNOW API
Other
51 stars 12 forks source link

Minknow_api in web with javascript #14

Open orel509 opened 3 years ago

orel509 commented 3 years ago

Hi, I want to use the minknow_api in web using javascript. There is no tutorial or example, and I can't seem to get it work on my own. Maybe you have some example I can learn from, or make a tutorial how to start using minknow_api on web? Also, I would like to get the right command for compiling the .proto files to javascript, as I think I miss some files because it outputs less files than the python files have. Hope to get an answer soon, and thanks in advance.

0x55555555 commented 3 years ago

Hi,

Thanks for your interest in the project, for starters with using javascript and the API there is: https://grpc.io/docs/languages/node/basics/

For talking to minknow_api from the frontend with no server, you can use protoc (https://developers.google.com/protocol-buffers/docs/reference/javascript-generated) + ts-protoc-gen (https://github.com/improbable-eng/ts-protoc-gen) to generate JS + TS outputs.

To make a request you can use the '@improbable-eng/grpc-web' library. An example may look something like:

import { grpc } from '@improbable-eng/grpc-web';
import { ManagerService } from 'compiled-proto-files/minknow_api/manager_pb_service';
import {
  FlowCellPositionsRequest,
  FlowCellPositionsResponse
} from 'compiled-proto-files/minknow_api/manager_pb';
const metadata = new grpc.Metadata();
grpc.unary(ManagerService.flow_cell_positions, {
  'https://localhost:9502',
  metadata,
  onEnd: (res) => {
    console.log(res);
    console.log(res.message?.toObject());
  },
  request: new FlowCellPositionsRequest()
});

Note that https will not function with the certificates MinKNOW uses in the browser, and you will likely see an error similar to NET::ERR_CERT_INVALID. To test this code in the browser you would need to start, in this case Google Chrome, with the flag '--ignore-certificate-errors'. We don't recommend keeping this flag on and that you should only use it when testing MinKNOW APIs in browser.

I hope that helps,

orel509 commented 3 years ago

I saw in the following tutorial https://grpc.io/docs/platforms/web/basics/ that an envoy proxy is needed. My question is, is it needed on the machine that hosts the minknow server(from where the user will enter the website), or on the machine hosting the website(which can be a remote machine)?

orel509 commented 3 years ago

Hi,

Thanks for your interest in the project, for starters with using javascript and the API there is: https://grpc.io/docs/languages/node/basics/

For talking to minknow_api from the frontend with no server, you can use protoc (https://developers.google.com/protocol-buffers/docs/reference/javascript-generated) + ts-protoc-gen (https://github.com/improbable-eng/ts-protoc-gen) to generate JS + TS outputs.

To make a request you can use the '@improbable-eng/grpc-web' library. An example may look something like:

import { grpc } from '@improbable-eng/grpc-web';
import { ManagerService } from 'compiled-proto-files/minknow_api/manager_pb_service';
import {
  FlowCellPositionsRequest,
  FlowCellPositionsResponse
} from 'compiled-proto-files/minknow_api/manager_pb';
const metadata = new grpc.Metadata();
grpc.unary(ManagerService.flow_cell_positions, {
  'https://localhost:9502',
  metadata,
  onEnd: (res) => {
    console.log(res);
    console.log(res.message?.toObject());
  },
  request: new FlowCellPositionsRequest()
});

Note that https will not function with the certificates MinKNOW uses in the browser, and you will likely see an error similar to NET::ERR_CERT_INVALID. To test this code in the browser you would need to start, in this case Google Chrome, with the flag '--ignore-certificate-errors'. We don't recommend keeping this flag on and that you should only use it when testing MinKNOW APIs in browser.

I hope that helps,

* George

that example doesnt work for me, I tried few other things and it all failed. I get an error "TypeError: Cannot read property 'responseStream' of undefined". Maybe you know how to solve it?

0x55555555 commented 3 years ago

Regarding the proxy: its not needed. MinKNOW uses the improbable-eng/grpc-web project and hosts the proxy already in port 9502 (our UI uses this to connect).

In terms of the examples not working more generally I am afraid I dont have any ideas - have you tried a more basic example with a self hosted grpc server?