4

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

Albert Lyubarsky
  • 438
  • 1
  • 7
  • 15

2 Answers2

3

I understand this is a bit old. But wanted to post this thing. I did transfer a file using C# grpc. Please see below a basic grpc file reque and response

  1. Protocol file
    syntax = "proto3";
    
    option csharp_namespace = "GrpcGreeter";
    
    package greet;
    
    // The greeting service definition. 
    
    service Greeter {
      // Sends a greeting

      rpc SayHello (HelloRequest) returns (HelloReply);
    }
    
    // The request message containing the user's name.

    message HelloRequest {
      string FileName = 1;
    }
    
    // The response message containing the greetings.

    message HelloReply {
      string FileName = 1;
      bytes data=2; 
    }

  1. gRPC service file
    using Google.Protobuf;
    
    using Grpc.Core;
    

    using GrpcGreeter;

    using static System.Net.Mime.MediaTypeNames;
    
    namespace GrpcGreeter.Services
    {
        

    public class GreeterService : Greeter.GreeterBase
        

    {

            private readonly ILogger<GreeterService> _logger;


            public GreeterService(ILogger<GreeterService> logger)
            {
                _logger = logger;
            }
    
            public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
            {
                return Task.FromResult(new HelloReply
                {
                    FileName =  request.FileName,
                    Data = ByteString.CopyFromUtf8(File.ReadAllText(@"D:\files\"+ request.FileName))
                });
            }
        }
    }
  1. gRPC file download
    using System.Threading.Tasks;
    
    
    using Grpc.Net.Client;
    
    
    using GrpcGreeterClient;
    
    
    
    
    // The port number must match the port of the gRPC server.
    using var channel = GrpcChannel.ForAddress("https://localhost:7092");
    
    
    var client = new Greeter.GreeterClient(channel);
    
    
    var reply = await client.SayHelloAsync(new HelloRequest { FileName = "test.txt" });
    
        
    File.WriteAllText(@"D:\files\"+"received"+reply.FileName, reply.Data.ToStringUtf8());
    
    
    Console.WriteLine("File written successfully. Press any key to exit...");
    
    
    Console.ReadKey();
MichaelMao
  • 2,596
  • 2
  • 23
  • 54
Kedar
  • 39
  • 3
  • Could you please try to improve the code formatting? Hint: the "{}" toolbar button will format the selected text as code. – Klaus Gütter Jan 08 '23 at 07:57
  • Sorry I'm very new to this I think @MichaelMao has edited it. The download part is a console application and using top level statements of C# – Kedar Jan 09 '23 at 12:10
  • I add the code block for you if you want you can change it again – MichaelMao Jan 09 '23 at 12:12
2

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

atish.s
  • 1,534
  • 11
  • 19