This page has moved to

Click here if you are not redirected.

Synchronous Subscriptions

Synchronous subscriptions require the application to wait for messages. This type of subscription is easy to set-up and use, but requires the application to deal with looping if multiple messages are expected. For situations where a single message is expected, synchronous subscriptions are sometimes easier to manage, depending on the language.

For example, to subscribe to the subject updates and receive a single message you could do:

nc, err := nats.Connect("")
if err != nil {
defer nc.Close()

// Subscribe
sub, err := nc.SubscribeSync("updates")
if err != nil {

// Wait for a message
msg, err := sub.NextMsg(10 * time.Second)
if err != nil {

// Use the response
log.Printf("Reply: %s", msg.Data)

Connection nc = Nats.connect("nats://");

// Subscribe
Subscription sub = nc.subscribe("updates");

// Read a message
Message msg = sub.nextMessage(Duration.ZERO);

String str = new String(msg.getData(), StandardCharsets.UTF_8);

// Close the connection
// node-nats subscriptions are always async.
# Asyncio NATS client currently does not have a sync subscribe API
# The Ruby NATS client subscriptions are all async.
// ts-nats subscriptions are always async.

results matching ""

    No results matching ""