Skip to content

TrogonStack/nats.pony

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nats.pony

A pure Pony client for NATS — no C dependencies, built entirely on lori TCP.

nats.pony implements the full NATS text protocol including MSG, HMSG (headers), INFO, PING/PONG, and error handling. It supports JetStream with durable consumers, ACK/NAK, and filter subjects. An incremental parser handles fragmented TCP delivery and binary payloads framed by byte count.

Pony's actor model is a natural fit for message-oriented systems, but connecting to NATS previously required FFI or external processes. nats.pony provides a native NatsClientActor trait that integrates directly with Pony's ASIO event loop — no shelling out, no C bindings, no unsafe code.

Built for Pony developers connecting actor-based services to NATS infrastructure. Whether you're consuming webhook events via JetStream or publishing messages between Pony actors and the rest of your stack, this library gives you a type-safe, zero-dependency NATS connection.

Installation

Add to your corral.json:

{
  "locator": "github.com/TrogonStack/nats.pony.git",
  "version": "0.1.0"
}

Then:

corral fetch

Usage

use "nats"
use lori = "lori"

actor MyNats is NatsClientActor
  var _nats: NatsClient = NatsClient.none()

  new create(auth: lori.TCPConnectAuth, config: NatsConfig val) =>
    _nats = NatsClient(auth, config, this)

  fun ref _nats_client(): NatsClient => _nats

  fun ref on_connected(info: NatsInfo val) =>
    _nats.subscribe("events.>", "1")

  fun ref on_message(msg: NatsMsg val) =>
    let payload = msg.payload_string()
    // process message

  fun ref on_error(message: String val) =>
    // handle error
    None

JetStream

fun ref on_connected(info: NatsInfo val) =>
  // Create a durable consumer
  let subjects: Array[String val] val = recover val ["github.>"] end
  let config = JetStream.consumer_config(
    "my-consumer", "_deliver.mine", subjects)
  _nats.publish_str(
    JetStream.consumer_create_subject("EVENTS"), config)
  _nats.subscribe("_deliver.mine", "js1")

fun ref on_message(msg: NatsMsg val) =>
  // Auto-ACK JetStream messages
  if (msg.reply_to.size() > 0) and JetStream.is_js_reply(msg.reply_to) then
    _nats.publish("$JS.ACK." + msg.reply_to, JetStream.ack_payload())
  end

Testing

corral fetch
corral run -- ponyc nats/test -o build -b nats-test -Dopenssl_3.0.x
./build/nats-test

23 tests (unit + property-based).

Dependencies

  • lori (TCP)
  • Pony 0.58+

License

MIT

About

Pure Pony NATS client — text protocol, JetStream, built on lori

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages