Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get client stream method return #1129

Closed
jucrouzet opened this issue Nov 9, 2022 · 3 comments
Closed

Get client stream method return #1129

jucrouzet opened this issue Nov 9, 2022 · 3 comments

Comments

@jucrouzet
Copy link

When using a client stream method like :

    rpc TestClientStream(stream visitons.backend.api.messages.String) returns (visitons.backend.api.messages.String);

How can I use the client to receive the returned value ?

RequestStream only offer the two following methods :

    on(type: 'end', handler: (status?: Status) => void): IRequestStream<T>;
    on(type: 'status', handler: (status: Status) => void): IRequestStream<T>;

none of them contains the returned value.

The returned value is returned like that (in Golang) :

	return stream.SendAndClose(&messages.String{
		String_: fmt.Sprintf("%d messages", count),
	})
@johanbrandhorst
Copy link
Contributor

You can see examples of how to use the streaming methods in the tests: https://github.com/improbable-eng/grpc-web/blob/master/integration_test/ts/src/client.websocket.spec.ts. I think you want to use onMessage.

@jucrouzet
Copy link
Author

Ok, thank you for this message, I understand the problem, and it seems to be in improbable-eng/ts-protoc-gen, not grpc-web.

For the method

    rpc TestClientStream(stream visitons.backend.api.messages.String) returns (visitons.backend.api.messages.String);

The js generated is :

APIClient.prototype.testClientStream = function testClientStream(metadata) {
  var listeners = {
    end: [],
    status: []
  };
  var client = grpc.client(API.TestClientStream, {
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport
  });
  client.onEnd(function (status, statusMessage, trailers) {
    listeners.status.forEach(function (handler) {
      handler({ code: status, details: statusMessage, metadata: trailers });
    });
    listeners.end.forEach(function (handler) {
      handler({ code: status, details: statusMessage, metadata: trailers });
    });
    listeners = null;
  });
  return {
    on: function (type, handler) {
      listeners[type].push(handler);
      return this;
    },
    write: function (requestMessage) {
      if (!client.started) {
        client.start(metadata);
      }
      client.send(requestMessage);
      return this;
    },
    end: function () {
      client.finishSend();
    },
    cancel: function () {
      listeners = null;
      client.close();
    }
  };
};

And the TS definition :

interface RequestStream<T> {
  write(message: T): RequestStream<T>;
  end(): void;
  cancel(): void;
  on(type: 'end', handler: (status?: Status) => void): RequestStream<T>;
  on(type: 'status', handler: (status: Status) => void): RequestStream<T>;
}

As you can see, onMessage is not handled.
I will create an issue there, thanks for your time.

@jucrouzet
Copy link
Author

Now fixed in improbable-eng/ts-protoc-gen#264

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants