glats

Types

pub type Connection =
  process.Subject(ConnectionMessage)

Message sent to a NATS connection process.

pub opaque type ConnectionMessage

Options that can be passed to connect.

pub type ConnectionOption {
  UserPass(String, String)
  Token(String)
  NKeySeed(String)
  JWT(String)
  CACert(String)
  ClientCert(String, String)
  InboxPrefix(String)
  ConnectionTimeout(Int)
  EnableNoResponders
}

Constructors

  • UserPass(String, String)

    Set a username and password for authentication.

  • Token(String)

    Set a token for authentication.

  • NKeySeed(String)

    Set an NKey seed for authentication.

  • JWT(String)

    Set a JWT for authentication.

  • 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.

Errors that can be returned by the server.

pub type Error {
  NoReplyTopic
  Timeout
  NoResponders
  Unexpected
}

Constructors

  • NoReplyTopic
  • Timeout
  • NoResponders
  • Unexpected

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

pub type Message {
  Message(
    topic: String,
    headers: dict.Dict(String, String),
    reply_to: option.Option(String),
    body: String,
  )
}

Constructors

  • Message(
      topic: String,
      headers: dict.Dict(String, String),
      reply_to: option.Option(String),
      body: String,
    )

Option that can be specified when publishing a message.

pub type PublishOption {
  ReplyTo(String)
  Headers(List(#(String, String)))
}

Constructors

  • ReplyTo(String)

    Provide a reply topic with the message that the receiver can send data back to.

  • Headers(List(#(String, String)))

    Headers to add to the message.

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.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.Option(Bool),
    )

Option that can be specified when subscribing to a topic.

pub type SubscribeOption {
  QueueGroup(String)
}

Constructors

  • QueueGroup(String)

    Subscribe to a topic as part of a queue group.

Message received by a subscribing subject.

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

Constructors

  • ReceivedMessage(
      conn: Connection,
      sid: Int,
      status: option.Option(Int),
      message: Message,
    )

Functions

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

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 new_inbox() -> String

Returns a new random inbox.

pub fn publish(
  conn: Subject(ConnectionMessage),
  topic: String,
  body: String,
  opts: List(PublishOption),
) -> Result(Nil, Error)

Publishes a single message to NATS on a provided topic.

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

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

pub fn request(
  conn: Subject(ConnectionMessage),
  topic: String,
  body: String,
  opts: List(PublishOption),
  timeout: Int,
) -> Result(Message, Error)

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 topic.

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,
  opts: List(PublishOption),
) -> Result(Nil, Error)

Sends a respond to a Message’s reply_to topic.

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

Returns server info provided by the connected NATS server.

pub fn subscribe(
  conn: Subject(ConnectionMessage),
  subscriber: Subject(SubscriptionMessage),
  topic: String,
  opts: List(SubscribeOption),
) -> Result(Int, Error)

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

To subscribe as a member of a queue group, add option QueueGroup(String) to list of options as the last parameter.

subscribe(conn, subject, "my.topic", [QueueGroup("my-group")])

See Queue Groups docs.

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

Unsubscribe from a subscription by providing the subscription ID.

Search Document