3

I have an api created via golang which return the result of

{
   id : drone4
   item_parameter : {
       altitude : 20,
       longitude : 10.20
      latitude : 24.5
}

as a json result, but in the code it uses go routines which call the go func() to process the result from grpc-server golang

like this

go func() {
        fmt.Print("start getPosition loop")
        for {
            msg, err := stream.Recv() // msg UAVPosition
            if err == io.EOF {
                // read done.
                fmt.Print("start getPosition loop closed")
                close(position)
                return
            }
            if err != nil {
                log.Fatalf("Failed to receive getPosition : %v", err)
                close(position)
                return
            }
            log.Printf("Position point[%s](%f, %f, %f)", uavID.Aircraft, msg.Latitude, msg.Longitude, msg.Altitude)

            itemParameter := models.ItemParameter{
                Latitude:  msg.Latitude,
                Longitude: msg.Longitude,
                Altitude:  msg.Altitude,
            }

            position <- models.DronePosition{
                Name:          uavID.Aircraft,
                ItemParameter: itemParameter,
            }
        }
    }()

I need to get the positon value which is i used this channel

position := make(chan models.DronePosition)

if any chance how can I get this result to stream to the ui using go? any suggestion how to work this streaming way in ui?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Yabetsu2018
  • 133
  • 1
  • 15

1 Answers1

4

If I understood you correctly -> this is server grpc endpoint and your problem is how to send models.DronePosition back to the client. If so, then you have bidirectional streaming and you can solve this problem as:

dronePosition := models.DronePosition{
                Name:          uavID.Aircraft,
                ItemParameter: itemParameter,
            }
position <- dronePosition
err = stream.Send(&dronePosition)

And on the client there should be the corresponding update to support bidirectional data streaming

Solorad
  • 904
  • 9
  • 21
  • this is the golang grpc goroutines and how can I write this code? currently I have this as ``` dronePos := <-position maybe ill add err = stream.Send(&dronePosition) and stream.CloseSend() ``` Please correct me where should i add this result? – Yabetsu2018 May 13 '19 at 03:09
  • 1
    Well, I see that you have here client streaming ( by calling `stream.Recv()` in a cycle. I don't know how your `.proto` file looks like, but you need for your task change it in a way that it supports bidirectional data streaming, regenerate service code (if it was not bidirectional) and add logic for returning answer via `err = stream.Send(&dronePosition)` – Solorad May 13 '19 at 03:16
  • This is my current code I changed i ``` itemParameter := models.ItemParameter{ Latitude: msg.Latitude, Longitude: msg.Longitude, Altitude: msg.Altitude, } dronePosition := models.DronePosition{ Name: uavID.Aircraft, ItemParameter: itemParameter, } position <- dronePosition ``` but the problem is how can I pass to my api? this is written in golang and also there is grpc server function that sends a message so my job is to return the response to api stream simultaneously – Yabetsu2018 May 13 '19 at 03:18
  • maube I don't understand your initial question. Do you want to pass it immediately? Then just turn it back in bidirectional way. If you need to pass it to another endpoint (some integration) -> then just put it for a while into DB – Solorad May 13 '19 at 03:21
  • what really happen my api must accept multiple request ``` stream API must have a compatible client from the UI. and multi client/object. meaning, it supports multiple stream from your API to multiple drone on the map single connection but supports multiple drone ``` which if i use get request this would be difficult to achieve also the streaming is happening in the grpc and also the api should stream as well (golang) am finding a way how to handle this and also as I ask a while a go, i searched last week on how to get result from the go routines which is difficult. – Yabetsu2018 May 13 '19 at 03:28
  • I suggest you to read about grpc. For example, there is a good example of bidirectional grpc calling: https://rakyll.org/grpc-streaming/ – Solorad May 13 '19 at 12:26