Search code examples
c#grpcgrpc-web

gRPC c# file download/upload + web client side


The main problem:

I am looking for a solution to improve upload/download speed for large files. I've heard about relatively new technology gRPC. I understand that it is good for server to server communication e.g. microservices architecture.

Client

However I need file upload/download component on front end (browser) ( something like Fine uploader, jQuery file upload plugin, Resumable.js) with gRPC support. I can do support by myself. But I don't know how and what and if it is possible at all. So I need an example or an advise or pointing to the right direction. No matter JS side: vanilla, react, angular ...

Server side

  • c# preferable
  • node.js possible
  • java workable

Research done on the subject

Please help or at least say that it is impossible


Solution

  • Yes it is possible to use gRPC to communicate between browser and a server. I would propose you to do a prove of concept before implementing the solution.

    gRPC uses protobuf to communicate and the data that needs to be communicated is encapsulated in a protobuf message.

    The first answer in gRPC Java File Download Example is correct. To be able to transfer files over a network you have to convert it into bytes.

    Using his example,

    message DataChunk {
       bytes data = 1;
    }
    
    service imageService {
       rpc DownloadProductImage(DownloadProductImageRequest) returns(stream DataChunk);
    }
    

    All this code should be in a protofile and the implementation should be generated using a protobuf generator (usually it is a maven plugin for java).

    The important point to note in the example provided is the word stream, this is what will enable you to send bytes to the server or vice versa. In the example he is downloading a stream of bytes from the service call DownloadProductImage.

    Here is a good starting point gRPC Java Basics, gRPC Concepts, Building a gRPC service with Java