nng

NNG (nanomsg-next-gen) lightweight messaging library bindings for Kit

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
c/kit_nng.cC FFI wrapper
c/kit_nng.hC header for FFI wrapper
examples/basic.kitBasic usage example
kit.tomlPackage manifest with metadata and dependencies
src/nng.kitKit NNG - Lightweight brokerless messaging library bindings
tests/nng.test.kitTests for nng

Dependencies

No Kit package dependencies.

Installation

kit add gitlab.com/kit-lang/packages/kit-nng.git

System Requirements

PlatformCommand
macOSbrew install nng
Ubuntusudo apt install libnng-dev
Fedorasudo dnf install nng-devel

Usage

import Kit.NNG as Nng

main = fn =>
  # Pair pattern (bidirectional)
  match Nng.pair-open
    | Ok server ->
      match Nng.listen server "ipc:///tmp/kit-nng-test"
        | Ok _ ->
          match Nng.pair-open
            | Ok client ->
              match Nng.dial client "ipc:///tmp/kit-nng-test"
                | Ok _ ->
                  # Client sends, server receives
                  match Nng.send client "Hello!"
                    | Ok _ -> println "Sent"
                    | Err e -> println ("Send error: " ++ e)

                  msg = Nng.recv server
                  println ("Received: " ++ msg)

                  match Nng.close client
                    | Ok _ -> no-op
                    | Err _ -> no-op
                | Err e -> println ("Dial error: " ++ e)
            | Err e -> println ("Client error: " ++ e)
          match Nng.close server
            | Ok _ -> no-op
            | Err _ -> no-op
        | Err e -> println ("Listen error: " ++ e)
    | Err e -> println ("Server error: " ++ e)

main

Development

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit

Compile examples to a native binary:

kit build examples/basic.kit && ./basic

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

Running kit dev

Run the standard development workflow (format, check, test):

kit dev

This will:

  1. Format and check source files in src/
  2. Run tests in tests/ with coverage

Generating Documentation

Generate API documentation from doc comments:

kit doc

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html

Cleaning Build Artifacts

Remove generated files, caches, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Local Installation

To install this package locally for development:

kit install

This installs the package to ~/.kit/packages/@kit/nng/, making it available for import as Kit.NNG in other projects.

License

This package is released under the MIT License - see LICENSE for details.

NNG is released under the MIT License.

Exported Functions & Types

req-open

Open a REQ (request) socket for request/reply pattern.

Returns:

Result Ptr String

rep-open

Open a REP (reply) socket for request/reply pattern.

Returns:

Result Ptr String

pub-open

Open a PUB (publisher) socket for pub/sub pattern.

Returns:

Result Ptr String

sub-open

Open a SUB (subscriber) socket for pub/sub pattern.

Returns:

Result Ptr String

push-open

Open a PUSH socket for pipeline pattern (send side).

Returns:

Result Ptr String

pull-open

Open a PULL socket for pipeline pattern (receive side).

Returns:

Result Ptr String

pair-open

Open a PAIR socket for bidirectional one-to-one communication.

Returns:

Result Ptr String

bus-open

Open a BUS socket for many-to-many communication.

Returns:

Result Ptr String

close

Close a socket and free its resources.

Parameters:

Returns:

Ptr -> Result Unit String

listen

Start listening for connections on a URL.

Parameters:

Returns:

Ptr -> NonEmptyString -> Result Unit String

dial

Connect to a remote URL.

Parameters:

Returns:

Ptr -> NonEmptyString -> Result Unit String

send

Send a string message on the socket.

Parameters:

Returns:

Ptr -> String -> Result Unit String

recv

Receive a string message from the socket (blocking).

Parameters:

Returns:

Ptr -> String

recv-timeout

Receive a string message with a timeout.

Parameters:

Returns:

Ptr -> PositiveInt -> Option String

sub-subscribe

Subscribe to a topic on a SUB socket.

Parameters:

Returns:

Ptr -> String -> Result Unit String

sub-unsubscribe

Unsubscribe from a topic on a SUB socket.

Parameters:

Returns:

Ptr -> String -> Result Unit String

set-recv-timeout

Set the receive timeout for a socket.

Parameters:

Returns:

Ptr -> PositiveInt -> Result Unit String

set-send-timeout

Set the send timeout for a socket.

Parameters:

Returns:

Ptr -> PositiveInt -> Result Unit String

version

Get the NNG library version string.

Returns:

String