{"record":{"id":"8ae9217cd6aeecba","repo":"micro/go-micro","slug":"error-while-obtaining-jetstream-context-w","errorCode":null,"errorMessage":"error while obtaining JetStream context: %w","messagePattern":"error while obtaining JetStream context: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"events/natsjs/nats.go","lineNumber":89,"sourceCode":"\tif options.Name != \"\" {\n\t\tnopts.Name = options.Name\n\t}\n\n\tif options.Username != \"\" && options.Password != \"\" {\n\t\tnopts.User = options.Username\n\t\tnopts.Password = options.Password\n\t}\n\n\tconn, err := nopts.Connect()\n\tif err != nil {\n\t\ttls := nopts.TLSConfig != nil\n\t\treturn nil, nil, fmt.Errorf(\"error connecting to nats at %v with tls enabled (%v): %w\", options.Address, tls, err)\n\t}\n\n\tjs, err := conn.JetStream()\n\tif err != nil {\n\t\tconn.Close() // Close connection if JetStream context fails\n\t\treturn nil, nil, fmt.Errorf(\"error while obtaining JetStream context: %w\", err)\n\t}\n\n\treturn conn, js, nil\n}\n\n// Publish a message to a topic.\nfunc (s *stream) Publish(topic string, msg interface{}, opts ...events.PublishOption) error {\n\t// validate the topic\n\tif len(topic) == 0 {\n\t\treturn events.ErrMissingTopic\n\t}\n\n\t// parse the options\n\toptions := events.PublishOptions{\n\t\tTimestamp: time.Now(),\n\t}\n\tfor _, o := range opts {\n\t\to(&options)","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/events/natsjs/nats.go#L71-L107","documentation":"This error occurs when the NATS connection succeeded but conn.JetStream() failed to obtain a JetStream context. The connection is closed before returning to avoid leaking sockets. It almost always means JetStream is disabled or unavailable on the connected server.","triggerScenarios":"Calling events/natsjs.NewStream against a NATS server started without JetStream enabled, or a server too old to support JetStream, or a leaf/super-cluster route where JetStream is not enabled for the account.","commonSituations":"Running stock NATS server without -js flag or enable_jetstream: true in config; connecting to an old NATS server version (<2.2); connecting to a monitoring-only node in a cluster that has no JetStream assets enabled for the account.","solutions":["Enable JetStream on the server: nats-server -js or set enable_jetstream: true in the config, then restart.","Upgrade the NATS server to >= 2.2 if it is older.","Verify with nats --server=<address> account info that JetStream is enabled for your account.","Check server logs for JetStream startup errors (e.g. storage directory not writable).","Retry NewStream after the server is corrected; the connection was already closed for you."],"exampleFix":"// before (server config)\n// nats-server -p 4222\n// after\n// nats-server -p 4222 -js --store_dir /data/jetstream","handlingStrategy":"fallback","validationCode":"nc, err := nats.Connect(addr)\nif err == nil {\n\tinfo := nc.ConnectedServerInfo()\n\tif !info.JetStream { return errors.New(\"server has JetStream disabled\") }\n\tnc.Close()\n}","typeGuard":"func isJetStreamDisabled(err error) bool { return strings.Contains(err.Error(), \"JetStream context\") }","tryCatchPattern":"conn, js, err := connectToNatsJetStream(opts)\nif err != nil {\n\tif strings.Contains(err.Error(), \"JetStream context\") {\n\t\tlog.Fatal(\"enable JetStream on the NATS server (-js) before starting\")\n\t}\n\treturn err\n}","preventionTips":["Start all NATS servers with -js / enable_jetstream: true by default.","Pin NATS server >= 2.2 in deployment manifests.","Add a startup probe that checks JetStream availability for the account."],"tags":["nats","jetstream","server-config"],"backgroundTag":"jetstream-not-enabled","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}