gocloud.dev: gocloud.dev/pubsub/natspubsub Index | Examples | Files

package natspubsub

import "gocloud.dev/pubsub/natspubsub"

Package natspubsub provides a pubsub implementation for NATS.io. Use OpenTopic to construct a *pubsub.Topic, and/or OpenSubscription to construct a *pubsub.Subscription. This package uses gob to encode and decode driver.Message to []byte.

URLs

For pubsub.OpenTopic and pubsub.OpenSubscription, natspubsub registers for the scheme "nats". The default URL opener will connect to a default server based on the environment variable "NATS_SERVER_URL". To customize the URL opener, or for more details on the URL format, see URLOpener. See https://gocloud.dev/concepts/urls/ for background information.

Message Delivery Semantics

NATS supports at-most-semantics; applications need not call Message.Ack, and must not call Message.Nack. See https://godoc.org/gocloud.dev/pubsub#hdr-At_most_once_and_At_least_once_Delivery for more background.

As

natspubsub exposes the following types for As:

- Topic: *nats.Conn
- Subscription: *nats.Subscription
- Message.BeforeSend: None.
- Message: *nats.Msg

Code:

// This example is used in https://gocloud.dev/howto/pubsub/subscribe/#nats

// import _ "gocloud.dev/pubsub/natspubsub"

// Variables set up elsewhere:
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".
subscription, err := pubsub.OpenSubscription(ctx, "nats://example.mysubject")
if err != nil {
    log.Fatal(err)
}
defer subscription.Shutdown(ctx)

Code:

// This example is used in https://gocloud.dev/howto/pubsub/publish/#nats

// import _ "gocloud.dev/pubsub/natspubsub"

// Variables set up elsewhere:
ctx := context.Background()

// pubsub.OpenTopic creates a *pubsub.Topic 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".
topic, err := pubsub.OpenTopic(ctx, "nats://example.mysubject")
if err != nil {
    log.Fatal(err)
}
defer topic.Shutdown(ctx)

Index

Examples

Package Files

nats.go

Constants

const Scheme = "nats"

Scheme is the URL scheme natspubsub registers its URLOpeners under on pubsub.DefaultMux.

func OpenSubscription Uses

func OpenSubscription(nc *nats.Conn, subject string, _ *SubscriptionOptions) (*pubsub.Subscription, error)

OpenSubscription returns a *pubsub.Subscription representing a NATS subscription. The subject is the NATS Subject to subscribe to; for more info, see https://nats.io/documentation/writing_applications/subjects.

TODO(dlc) - Options for queue groups?

Code:

// This example is used in https://gocloud.dev/howto/pubsub/subscribe/#nats-ctor

// Variables set up elsewhere:
ctx := context.Background()

natsConn, err := nats.Connect("nats://nats.example.com")
if err != nil {
    log.Fatal(err)
}
defer natsConn.Close()

subscription, err := natspubsub.OpenSubscription(
    natsConn,
    "example.mysubject",
    nil)
if err != nil {
    log.Fatal(err)
}
defer subscription.Shutdown(ctx)

func OpenTopic Uses

func OpenTopic(nc *nats.Conn, subject string, _ *TopicOptions) (*pubsub.Topic, error)

OpenTopic returns a *pubsub.Topic for use with NATS. The subject is the NATS Subject; for more info, see https://nats.io/documentation/writing_applications/subjects.

Code:

// This example is used in https://gocloud.dev/howto/pubsub/publish/#nats-ctor

// Variables set up elsewhere:
ctx := context.Background()

natsConn, err := nats.Connect("nats://nats.example.com")
if err != nil {
    log.Fatal(err)
}
defer natsConn.Close()

topic, err := natspubsub.OpenTopic(natsConn, "example.mysubject", nil)
if err != nil {
    log.Fatal(err)
}
defer topic.Shutdown(ctx)

type SubscriptionOptions Uses

type SubscriptionOptions struct{}

SubscriptionOptions sets options for constructing a *pubsub.Subscription backed by NATS.

type TopicOptions Uses

type TopicOptions struct{}

TopicOptions sets options for constructing a *pubsub.Topic backed by NATS.

type URLOpener Uses

type URLOpener struct {
    // Connection to use for communication with the server.
    Connection *nats.Conn
    // TopicOptions specifies the options to pass to OpenTopic.
    TopicOptions TopicOptions
    // SubscriptionOptions specifies the options to pass to OpenSubscription.
    SubscriptionOptions 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 Uses

func (o *URLOpener) OpenSubscriptionURL(ctx context.Context, u *url.URL) (*pubsub.Subscription, error)

OpenSubscriptionURL opens a pubsub.Subscription based on u.

func (*URLOpener) OpenTopicURL Uses

func (o *URLOpener) OpenTopicURL(ctx context.Context, u *url.URL) (*pubsub.Topic, error)

OpenTopicURL opens a pubsub.Topic based on u.

Package natspubsub imports 15 packages (graph) and is imported by 6 packages. Updated 2019-07-18. Refresh now. Tools for package owners.