Documentation ¶
Overview ¶
Example (OpenSubscriptionFromURL) ¶
package main import ( "context" "log" "gocloud.dev/pubsub" ) func main() { // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" // PRAGMA: On gocloud.dev, hide lines until the next blank line. ctx := context.Background() // pubsub.OpenSubscription creates a *pubsub.Subscription from a URL. // This URL will Dial the NATS server at the URL in the environment variable // NATS_SERVER_URL and receive messages with subject "example.mysubject". // This URL will be parsed and the natsv2 attribute will be used to // use NATS v2.2.0+ native message headers as the message metadata. subscription, err := pubsub.OpenSubscription(ctx, "nats://nats.example.com/example.mysubject?jetstream=true") if err != nil { log.Fatal(err) } defer func(subscription *pubsub.Subscription, ctx context.Context) { _ = subscription.Shutdown(ctx) }(subscription, ctx) }
Output:
Example (OpenTopicFromURL) ¶
package main import ( "context" "log" "gocloud.dev/pubsub" ) func main() { // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" // PRAGMA: On gocloud.dev, hide lines until the next blank line. ctx := context.Background() // pubsub.OpenTopic creates a *pubsub.Connection from a URL. // This URL will Dial the NATS server at the URL in the environment variable // NATS_SERVER_URL and send messages with subject "example.mysubject". // This URL will be parsed and the natsv2 attribute will be used to // use NATS v2.2.0+ native message headers as the message metadata. topic, err := pubsub.OpenTopic(ctx, "nats://nats.example.com/example.mysubject") if err != nil { log.Fatal(err) } defer func(topic *pubsub.Topic, ctx context.Context) { _ = topic.Shutdown(ctx) }(topic, ctx) }
Output:
Index ¶
Examples ¶
Constants ¶
const Scheme = "nats"
Scheme is the URL scheme natspubsub registers its URLOpeners under on pubsub.DefaultMux.
Variables ¶
This section is empty.
Functions ¶
func OpenSubscription ¶
func OpenSubscription(ctx context.Context, conn connections.Connection, opts *connections.SubscriptionOptions) (*pubsub.Subscription, error)
OpenSubscription returns a *pubsub.Subscription representing a NATS subscription or NATS queue subscription for use with NATS at least version 2.2.0. This changes the encoding of the message as, starting with version 2.2.0, NATS supports message headers. In previous versions the message headers were encoded along with the message content using gob.Encoder, which limits the subscribers only to Go clients. This implementation uses native NATS message headers, and native message content, which provides full support for non-Go clients.
Example ¶
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" // PRAGMA: On gocloud.dev, hide lines until the next blank line. ctx := context.Background() natsConn, err := nats.Connect("nats://nats.example.com") if err != nil { log.Fatal(err) } defer natsConn.Close() conn := connections.NewPlain(natsConn) subscription, err := natspubsub.OpenSubscription( ctx, conn, &connections.SubscriptionOptions{Subjects: []string{"example.mysubject"}}) if err != nil { log.Fatal(err) } defer func(subscription *pubsub.Subscription, ctx context.Context) { _ = subscription.Shutdown(ctx) }(subscription, ctx)
Output:
func OpenTopic ¶
func OpenTopic(ctx context.Context, conn connections.Connection, opts *connections.TopicOptions) (*pubsub.Topic, error)
OpenTopic returns a *pubsub.Topic for use with NATS at least version 2.2.0. This changes the encoding of the message as, starting with version 2.2.0, NATS supports message headers. In previous versions the message headers were encoded along with the message content using gob.Encoder, which limits the subscribers only to Go clients. This implementation uses native NATS message headers, and native message content, which provides full support for non-Go clients.
Example ¶
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub" // PRAGMA: On gocloud.dev, hide lines until the next blank line. ctx := context.Background() natsConn, err := nats.Connect("nats://nats.example.com") if err != nil { log.Fatal(err) } defer natsConn.Close() js, err := jetstream.New(natsConn) if err != nil { log.Fatal(err) } conn := connections.NewJetstream(js) topic, err := natspubsub.OpenTopic(ctx, conn, &connections.TopicOptions{Subject: "example.mysubject"}) if err != nil { log.Fatal(err) } defer func(topic *pubsub.Topic, ctx context.Context) { _ = topic.Shutdown(ctx) }(topic, ctx)
Output:
Types ¶
type URLOpener ¶
type URLOpener struct { Connection connections.Connection // TopicOptions specifies the options to pass to OpenTopic. TopicOptions connections.TopicOptions // SubscriptionOptions specifies the options to pass to OpenSubscription. SubscriptionOptions connections.SubscriptionOptions }
URLOpener opens NATS URLs like "nats://mysubject".
The URL host+path is used as the subject.
No query parameters are supported.
func (*URLOpener) OpenSubscriptionURL ¶
func (o *URLOpener) OpenSubscriptionURL(ctx context.Context, u *url.URL) (*pubsub.Subscription, error)
OpenSubscriptionURL opens a pubsub.Subscription based on url supplied.
A subscription also creates the required underlaying queue or streams There are many more parameters checked in this case compared to the publish topic section. If required the list of parameters can be extended but for now only a subset is defined and the remaining ones utilize the sensible defaults that nats comes with. The list of parameters include : - subject, - stream_name, - stream_description, - stream_subjects, - consumer_max_count, - consumer_queue - consumer_max_waiting -
func (*URLOpener) OpenTopicURL ¶
OpenTopicURL opens a pubsub.Topic based on a url supplied.
A topic can be specified in the subject and suffixed by the url path These definitions will yield the subject shown infront of them - nats://host:8934?subject=foo --> foo - nats://host:8934/bar?subject=foo --> foo/bar - nats://host:8934/bar --> /bar - nats://host:8934?no_subject=foo --> [this yields an error]