{"record":{"id":"4aca5301ca71760e","repo":"micro/go-micro","slug":"error-subscribing-to-topic","errorCode":null,"errorMessage":"Error subscribing to topic","messagePattern":"Error subscribing to topic","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"events/natsjs/nats.go","lineNumber":270,"sourceCode":"\t} else {\n\t\tsubOpts = append(subOpts, nats.DeliverNew())\n\t}\n\n\tif options.AckWait > 0 {\n\t\tsubOpts = append(subOpts, nats.AckWait(options.AckWait))\n\t}\n\n\t// connect the subscriber via a queue group only if durable streams are enabled\n\tif !s.opts.DisableDurableStreams {\n\t\tsubOpts = append(subOpts, nats.Durable(options.Group))\n\t\t_, err = s.natsJetStreamCtx.QueueSubscribe(topic, options.Group, handleMsg, subOpts...)\n\t} else {\n\t\tsubOpts = append(subOpts, nats.ConsumerName(options.Group))\n\t\t_, err = s.natsJetStreamCtx.Subscribe(topic, handleMsg, subOpts...)\n\t}\n\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"Error subscribing to topic\")\n\t}\n\n\treturn channel, nil\n}\n\n// Close implements io.Closer and closes the underlying NATS connection.\n// This method is optional but recommended to prevent connection leaks.\nfunc (s *stream) Close() error {\n\tif s.conn != nil {\n\t\ts.conn.Close()\n\t\ts.conn = nil\n\t}\n\treturn nil\n}\n\n// Ensure stream implements io.Closer\nvar _ io.Closer = (*stream)(nil)\n","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/events/natsjs/nats.go#L252-L288","documentation":"After ensuring the stream exists, Consume subscribes either via QueueSubscribe (durable streams enabled) or Subscribe, attaching the message handler and subscription options. This error wraps any NATS failure returned from creating that subscription.","triggerScenarios":"Calling Consume when the subscription fails: a durable consumer with the same name already exists with a different configuration, the topic/stream was deleted between the StreamInfo check and Subscribe, or the connection dropped before the subscribe completed.","commonSituations":"Changing ConsumeOptions (AckWait, MaxDeliver, start time) while a durable consumer of the same name exists — NATS rejects reconfiguring durables; restarting a service against a stale durable consumer; NATS connection loss during startup.","solutions":["Read errors.Cause(err) to see the NATS subscription failure (e.g. consumer name already in use)","Delete the stale durable consumer (nats consumer rm or js.DeleteConsumer) when its config changed","Keep Consume options (Group, AutoAck, AckWait, retries) stable across deployments, or change Group name","Ensure the NATS connection is healthy before calling Consume","Verify the stream for the topic still exists at subscribe time"],"exampleFix":"// before\n// changed AckWait but old durable \"order-svc\" exists -> consumer name already in use\n_, err = js.QueueSubscribe(topic, \"order-svc\", handleMsg, subOpts...)\n// after\njs.DeleteConsumer(streamName, \"order-svc\") // remove stale durable once before resubscribing\n_, err = js.QueueSubscribe(topic, \"order-svc\", handleMsg, subOpts...)","handlingStrategy":"retry","validationCode":"// ensure connection and stream are healthy before subscribing\nif !conn.IsConnected() { return errors.New(\"nats not connected\") }\nif _, err := js.StreamInfo(topic); err != nil { return err }","typeGuard":null,"tryCatchPattern":"ch, err := stream.Consume(topic, events.Group(\"order-svc\"))\nif err != nil {\n    if strings.Contains(err.Error(), \"in use\") || strings.Contains(err.Error(), \"exists\") {\n        // delete stale durable and retry once\n        js.DeleteConsumer(topic, \"order-svc\")\n        ch, err = stream.Consume(topic, events.Group(\"order-svc\"))\n    }\n    if err != nil { return err }\n}","preventionTips":["Keep consume options and Group names stable across releases","Delete stale durable consumers when changing ack/wait/retry configuration","Verify connectivity before calling Consume","Enable durable streams consistently (DisableDurableStreams setting) across environments"],"tags":["nats","jetstream","subscription","durable-consumer"],"backgroundTag":"nats-subscribe-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}