Lightweight Toolkit for Messaging, Concurrency and the Web

R binding for NNG (Nanomsg Next Gen), a successor to ZeroMQ. A toolkit for messaging, concurrency and the web. High-performance socket messaging over in-process, IPC, TCP, WebSocket and secure TLS transports implements 'Scalability Protocols', a standard for common communications patterns including publish/subscribe, request/reply and survey. A threaded concurrency framework with intuitive 'aio' objects that resolve automatically upon completion of asynchronous operations, and synchronisation primitives that allow R to wait on events signalled by concurrent threads. A unified HTTP server hosting REST endpoints, WebSocket connections and streaming on a single port, with a built-in HTTP client.


nanonext nanonext logo

CRANstatus R-universestatus R-CMD-check Codecov testcoverage

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

AskDeepWiki

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_aio("async request")

# 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)) run_event_loop(1000)
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

Uses a system ‘libnng’ >= v1.12.3 and ‘libmbedtls’ v3.x if present, otherwise compiles the bundled sources (libnng v1.12.3, libmbedtls v3.6.5) directly into the package - requiring only a C compiler.

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. The bundled libraries are compiled directly by Rtools - no additional tools are needed.

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.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("nanonext")

1.10.3 by Charlie Gao, 23 days ago


https://nanonext.r-lib.org, https://github.com/r-lib/nanonext


Report a bug at https://github.com/r-lib/nanonext/issues


Browse source code at https://github.com/cran/nanonext


Authors: Charlie Gao [aut, cre] , Posit Software , PBC [cph, fnd] (ROR: <https://ror.org/03wc8by49>) , Hibiki AI Limited [cph] , Staysail Systems , Inc. [cph] (NNG library) , Capitar IT Group BV [cph] (NNG library) , The Mbed TLS Contributors [cph] (Mbed TLS library) , Pierre L'Ecuyer [cph] (RngStreams library) , sakura authors [cph] (Serialization hooks) , R Consortium [fnd] (ROR: <https://ror.org/01z833950>)


Documentation:   PDF Manual  


MIT + file LICENSE license


Suggests later, litedown, yaml

Enhances promises

System requirements: 'libnng' >= 1.12 and 'libmbedtls' >= 3.0 (optional, bundled sources compiled otherwise)


Imported by autosync, crew, crew.cluster, ichimoku, mcplite, mcptools, mirai, mochita.

Suggested by httr2, later, pkgdown, rtemis, shinyGovstyle, targets.


See at CRAN