This API documentation is for Ballerina 1.2.0. View API documentation for the latest release.

Clients - nats : Producer

The producer provides the capability to publish messages to the NATS server. The nats:Producer needs the nats:Connection to be initialized.

Constructor

__init

(Connection connection)

  • connection Connection
  • An established NATS connection

Remote Methods

publish

Publishes data to a given subject.

request

Publishes data to a given subject and waits for a response.

Methods

Closes a given connection.

publish

(string subject, Content data, string | service | () replyTo)

returns Error?

Publishes data to a given subject.

 nats:Error? result = producer->publish(subject, <@untainted>message);

Parameters

  • subject string
  • The subject to send the message

  • replyTo string | service | () (default ())
  • The subject or the callback service to which the receiver should send the response

  • Return Type

    (Error?)
  • () or else a nats:Error if there is a problem when publishing the message

request

(string subject, Content data, int? duration)

returns Message | Error

Publishes data to a given subject and waits for a response.

 nats:Message|nats:Error reqReply = producer->request(subject, <@untainted>message, 5000);

Parameters

  • subject string
  • The subject to send the message

  • duration int? (default ())
  • The time (in milliseconds) to wait for the response

  • Return Type

    (Message | Error)
  • The nats:Message response or else a nats:Error if an error is encountered

close

()

returns Error?

Closes a given connection.

  • Return Type

    (Error?)
  • () or else a nats:Error if unable to complete the close the operation