nats-io / nats.go

Golang client for NATS, the cloud native messaging system.
https://nats.io
Apache License 2.0
5.41k stars 685 forks source link

QueueSubscribe Durability #526

Closed mbecker closed 4 years ago

mbecker commented 4 years ago

Hi, as a NATS rookie I'm a little bit confused about the different version for the Go client library. I'm looking to have a QueueSubscribe with durable message subscripton.

Lookin at the following URLs I do see the code for that but with the nats.go clent that is not possible: (1) https://nats.io/blog/use-cases-for-persistent-logs-with-nats-streaming/

handle := func(msg *stan.Msg) {
    // ...
}

conn.QueueSubscribe(
    "stream-name",
    "queue-name",
    handle,
    // options: durable, manual ack, etc.
)

(2) https://github.com/nats-io/nats-streaming-server/blob/b549227a16bcf5cf7c7544e818249760266edcea/server/server_queue_test.go#L418

sub, err := sc1.QueueSubscribe("foo", "group", cb,
        stan.DeliverAllAvailable(),
        stan.DurableName("qsub"))
    if err != nil {
        t.Fatalf("Unexpected error on subscribe: %v", err)
    }

My code with the snippets from above produces the following error:

./main.go:76:28: too many arguments in call to nc.QueueSubscribe
    have (string, string, func(*nats.Msg), stan.SubscriptionOption)
    want (string, string, nats.MsgHandler)

Am I missing anything? Thanks for your help!

kozlovic commented 4 years ago

You need to look at stan.go instead: https://github.com/nats-io/stan.go

mbecker commented 4 years ago

Hi, thanks! Looks what I'm looking for. May I ask what is the different of "stan.go" and "nats.go". Why both client libraries?

wallyqs commented 4 years ago

@mbecker stan.go uses the nats.go client library to be able to use NATS Streaming, which has a similar API to NATS but enhanced with persistence of messages. If you are looking for persistence of messages then you could start with NATS Streaming, for real-time communications and request/response then you could start with NATS. Here is more info about the relation of NATS (nats.go) and NATS Streaming (stan.go or STAN): https://nats-io.github.io/docs/developer/streaming/

mbecker commented 4 years ago

Hi @wallyqs , thanks for the detailed explanation. I highly appreciate that! I missed the following sentence on the page: "Sometimes the maintainers will refer to NATS as "nats core" and streaming as "stan" or "streaming"."

mmm8955405 commented 11 months ago

Sorry, please reply to this question now. I didn't see any more information about persistence in the document. Can stream persistence ensure that no message is lost? Many businesses have high requirements for persistence, which is a concern for programmers

mmm8955405 commented 11 months ago

I need the program to not lose any messages. Can NATS do this?

derekcollison commented 11 months ago

Yes, please visit https://docs.nats.io/nats-concepts/jetstream