glats

Types

pub type Connection =
  Subject(ConnectionMessage)

Errors that can be returned by the server.

pub type ConnectionError {
  Timeout
  NoResponders
  Unexpected
}

Constructors

  • Timeout
  • NoResponders
  • Unexpected

Message sent to a NATS connection process.

pub opaque type ConnectionMessage

Options that can be passed to connect.

pub type ConnectionOption {
  CACert(String)
  ClientCert(String, String)
  InboxPrefix(String)
  ConnectionTimeout(Int)
  EnableNoResponders
}

Constructors

  • CACert(String)

    Set a CA cert for the server connection.

  • ClientCert(String, String)

    Set client certificate key pair for authentication.

  • InboxPrefix(String)

    Set a custom inbox prefix used by the connection.

  • ConnectionTimeout(Int)

    Set a connection timeout in milliseconds.

  • EnableNoResponders

    Enable the no responders behavior.

A single message that can be received from or sent to NATS.

pub type Message {
  Message(
    subject: String,
    headers: Map(String, String),
    reply_to: Option(String),
    body: String,
  )
}

Constructors

  • Message(
      subject: String,
      headers: Map(String, String),
      reply_to: Option(String),
      body: String,
    )

Server info returned by the NATS server.

pub type ServerInfo {
  ServerInfo(
    server_id: String,
    server_name: String,
    version: String,
    go: String,
    host: String,
    port: Int,
    headers: Bool,
    max_payload: Int,
    proto: Int,
    jetstream: Bool,
    auth_required: Option(Bool),
  )
}

Constructors

  • ServerInfo(
      server_id: String,
      server_name: String,
      version: String,
      go: String,
      host: String,
      port: Int,
      headers: Bool,
      max_payload: Int,
      proto: Int,
      jetstream: Bool,
      auth_required: Option(Bool),
    )

Message received by a subscribing subject.

pub type SubscriptionMessage {
  ReceivedMessage(conn: Connection, sid: Int, message: Message)
}

Constructors

  • ReceivedMessage(conn: Connection, sid: Int, message: Message)

Functions

pub fn active_subscriptions(conn: Subject(ConnectionMessage)) -> Result(
  Int,
  ConnectionError,
)

Returns the number of active subscriptions for the connection.

pub fn connect(host: String, port: Int, opts: List(
    ConnectionOption,
  )) -> Result(Subject(ConnectionMessage), StartError)

Starts an actor that handles a connection to NATS using the provided settings.

Example

connect(
  "localhost",
  4222,
  [
    CACert("/tmp/nats/ca.crt"),
    InboxPrefix("_INBOX.custom.prefix."),
  ],
)
pub fn publish(conn: Subject(ConnectionMessage), subject: String, message: String) -> Result(
  Nil,
  String,
)

Publishes a single message to NATS on a provided subject.

pub fn publish_message(conn: Subject(ConnectionMessage), message: Message) -> Result(
  Nil,
  String,
)

Publishes a single message to NATS using the data from a provided Message record.

pub fn queue_subscribe(conn: Subject(ConnectionMessage), subscriber: Subject(
    SubscriptionMessage,
  ), subject: String, group: String) -> Result(Int, String)

Subscribes to a NATS subject as part of a queue group. Messages can be received on the provided OTP subject.

See Queue Groups docs.

pub fn request(conn: Subject(ConnectionMessage), subject: String, message: String, timeout: Int) -> Result(
  Message,
  ConnectionError,
)

Sends a request and listens for a response synchronously. When connection is established with option EnableNoResponders, Error(NoResponders) will be returned immediately if no subscriber exists for the subject.

See request-reply pattern docs.

To handle a request from NATS see handler.handle_request.

pub fn respond(conn: Subject(ConnectionMessage), message: Message, body: String) -> Result(
  Nil,
  String,
)

Sends a respond to a Message’s reply_to subject.

pub fn server_info(conn: Subject(ConnectionMessage)) -> Result(
  ServerInfo,
  ConnectionError,
)

Returns server info provided by the connected NATS server.

pub fn subscribe(conn: Subject(ConnectionMessage), subscriber: Subject(
    SubscriptionMessage,
  ), subject: String) -> Result(Int, String)

Subscribes to a NATS subject that can be received on the provided OTP subject.

pub fn unsubscribe(conn: Subject(ConnectionMessage), sid: Int) -> Result(
  Nil,
  String,
)

Unsubscribe from a subscription by providing the subscription ID.

Search Document