1

I am trying to work with gRPC in my electron application but grpc module is not working after building the electron application. Initially I tried with grpc node module but it is deprecated recently. So I tried with @grpc/grpc.js module and it is not working as expected. Below are the steps what I have done so far

  1. npm install @grpc/grpc.js @grpc/grpc-protoloader.
  2. imported both grpc modules
  3. added grpc related code in my electron app.
  4. added .proto file
  5. after all the setup I tried to launch my electron application and getting the below error.

ERROR Error: Uncaught (in promise): Error: Invalid package C:\Users\some-user\Desktop\MockServer\Trilogy_Client_Installer\node_modules\electron\dist\resources\electron.asar

code:

import { ElectronService } from './electron.service';
import { Injectable } from '@angular/core';
import {ProtoGrpcType} from './../../../../proto/periPheral';
import { PeriPheralManagerHandlers } from './../../../../proto/periPheralPackage/PeriPheralManager';
import { TodoResponse } from './../../../../proto/periPheralPackage/TodoResponse';


@Injectable({
    providedIn: 'root',
})


export class GrpcService {

    private PORT = 5002
    private PROTO_FILE = './proto/periPheral.proto'
    private packageDef = this.electronService.protoLoader.loadSync(this.electronService.path.resolve(__dirname, this.PROTO_FILE))
    private grpcObj = (this.electronService.grpc.loadPackageDefinition(this.packageDef) as unknown) as ProtoGrpcType
    private periPheralPackage = this.grpcObj.periPheralPackage;
    constructor(private electronService: ElectronService) { }
 
    main() {
        // const server = this.getServer()
        const server = new this.electronService.grpc.Server()
        server.addService(this.periPheralPackage.PeriPheralManager.service, {
            PingPong: this.PingPong
        }as PeriPheralManagerHandlers);
        server.bindAsync(`http://localhost:${this.PORT}`, this.electronService.grpc.ServerCredentials.createInsecure(),
        (err, port) => {
          if (err) {
            console.error(err)
            return
          }
          console.log(`Your server as started on port ${port}`)
          server.start()
        })

      }

      // mention below any method in addService second param.
       PingPong(call, callback) {
        callback(null, { message: 'Welcome ' + call.request.name });
      } }
Neha
  • 15
  • 4
  • This has been resolved. The error happened because I did not set the proper path to the proto files in my code. – Neha May 21 '23 at 09:21

0 Answers0