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

How to create and pass instances of new NatsMsg<T>() to the NatsConnection#PublishAsync<T>(in NatsMsg<T> msg....) API correctly? #632

Open
Farrukhjon opened this issue Sep 16, 2024 · 1 comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Farrukhjon
Copy link

Farrukhjon commented Sep 16, 2024

Proposed change

I want to use this API

NatsConnection#public ValueTask PublishAsync<T>(in NatsMsg<T> msg, INatsSerialize<T>? serializer = default, NatsPubOpts? opts = default, CancellationToken cancellationToken = default) =>
        PublishAsync(msg.Subject, msg.Data, msg.Headers, msg.ReplyTo, serializer, opts, cancellationToken);

to publish instances of NatsMsg<T>

But this requires from me to construct an instance of NatsMsg in "low level" manner:

new NatsMsg<T>(
                Subject: subject,
                Data: payload,
                Headers: headers,
                ReplyTo: replyTo,
                Connection: connection,
                Size: size
            );

If I understood correctly, API's doc hints to provide the Size of the message in such a way:

 int size = subject.Length + replyTo.Length + headers.Length + payload.Length;

But to achieve this, it seems, one needs to provide the headers parsed (since headers.Length is not compilable).

Question: How to create and pass instances of NatsMsg<T>() to the NatsConnection#PublishAsync<T>(in NatsMsg<T> msg....) API correctly?

I'm asking such question since I used nats-java version of this API but now want to transfer the java code consistently (without loosing API usage semantic) to C#/.Net platform.
https://github.com/nats-io/nats.java/blob/main/src/main/java/io/nats/client/impl/NatsMessage.java#L447

Message message = NatsMessage.builder()
            .subject(subject)
            .headers(headers)
            .replyTo(replyTo)
            .data(payload)
            .build();     
// and then publish it:
connection.jetStream().publish(message)            

Note that a user of the Nats Java API is not enforced to provide "Size" of the message up front.

Use case

Better user experience to call NatsConnection#PublishAsync<T>(in NatsMsg<T> msg....) API.

Contribution

No response

@mtmk
Copy link
Collaborator

mtmk commented Sep 16, 2024

currently publish(NatsMsg) call implementation doesn't need all the fields and defaults would be fine:

new NatsMsg<T>(
                Subject: subject,
                Data: payload,
                Headers: headers,
                ReplyTo: replyTo,
                Connection: default,
                Size: default
            );

it'd be nice to formalize it with a builder and tests.

@mtmk mtmk added good first issue Good for newcomers help wanted Extra attention is needed labels Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants