Skip to content
/ nanonext Public

R binding for NNG (Nanomsg Next Gen)

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Unknown
LICENSE.note
Notifications You must be signed in to change notification settings

r-lib/nanonext

nanonext nanonext logo

CRAN status R-universe status R-CMD-check Codecov test coverage

Fast, lightweight toolkit for messaging, concurrency, and the web in R. Built on NNG (Nanomsg Next Gen) and implemented almost entirely in C.

  • Scalability protocols - pub/sub, req/rep, push/pull, surveyor/respondent, bus, pair
  • Multiple transports - TCP, IPC, WebSocket, TLS, in-process
  • Async I/O - non-blocking operations with auto-resolving ‘aio’ objects
  • Cross-language - exchange data with Python, C++, Go, Rust
  • Web toolkit - unified HTTP, WebSocket, and streaming (SSE, NDJSON) on a single port

Quick Start

library(nanonext)

# Open sockets
s1 <- socket("req", listen = "ipc:///tmp/nanonext")
s2 <- socket("rep", dial = "ipc:///tmp/nanonext")

# Send
s1 |> send("hello world")
#> [1] 0

# Receive on the other
s2 |> recv()
#> [1] "hello world"

close(s1)
close(s2)

Async I/O

Non-blocking operations that resolve automatically:

s1 <- socket("rep", listen = "tcp://127.0.0.1:5556")
s2 <- socket("req", dial = "tcp://127.0.0.1:5556")

# Sender
s2 |> send("async request")
#> [1] 0

# Async operations return immediately
aio <- recv_aio(s1)
aio
#> < recvAio | $data >

# Retrieve result when ready
aio$data
#> [1] "async request"

close(s1)
close(s2)

Web Toolkit

One server, one port – HTTP endpoints, WebSocket connections, and streaming all coexist. Mbed TLS built in for HTTPS/WSS.

# Generate self-signed certificates
cert <- write_cert(cn = "127.0.0.1")

# HTTPS server (port 0 = auto-assign a free port)
server <- http_server(
  url = "https://127.0.0.1:0",
  handlers = list(
    handler("/", \(req) list(status = 200L, body = '{"status":"ok"}'))
  ),
  tls = tls_config(server = cert$server)
)
server$start()

# Async HTTPS client
aio <- ncurl_aio(server$url, tls = tls_config(client = cert$client))
while (unresolved(aio)) later::run_now(1)
aio$data
#> [1] "{\"status\":\"ok\"}"

server$close()

Documentation

Guide Topics
Quick Reference At-a-glance API overview
Messaging Cross-language, async, synchronisation
Protocols req/rep, pub/sub, surveyor/respondent
Configuration TLS, options, serialization
Web Toolkit HTTP client/server, WebSocket, streaming

Installation

# CRAN
install.packages("nanonext")

# Development version
install.packages("nanonext", repos = "https://r-lib.r-universe.dev")

Building from Source

Linux / Mac / Solaris

Requires ‘libnng’ >= v1.9.0 and ‘libmbedtls’ >= 2.5.0, or ‘cmake’ to compile bundled libraries (libnng v1.11.0, libmbedtls v3.6.5).

Recommended: Let the package compile bundled libraries for optimal performance:

Sys.setenv(NANONEXT_LIBS = 1)
install.packages("nanonext")

System packages: libnng-dev / nng-devel, libmbedtls-dev / libmbedtls-devel. Set INCLUDE_DIR and LIB_DIR for custom locations.

Windows

Requires Rtools. For R >= 4.2, cmake is included. Earlier versions need cmake installed separately and added to PATH.

Links

Documentation | NNG | Mbed TLS | CRAN HPC Task View | CRAN Web Technologies

Acknowledgements

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.