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
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)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)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()| 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 |
# CRAN
install.packages("nanonext")
# Development version
install.packages("nanonext", repos = "https://r-lib.r-universe.dev")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.
Requires Rtools. For R >= 4.2, cmake is included. Earlier versions need cmake installed separately and added to PATH.
Documentation | NNG | Mbed TLS | CRAN HPC Task View | CRAN Web Technologies
- Garrett D’Amore (NNG author) for advice and implementing features for nanonext
- R Consortium for funding TLS development, with support from Henrik Bengtsson and Will Landau
- Joe Cheng for prototyping event-driven promises integration
- Luke Tierney and Mike Cheng for R serialization documentation
- Travers Ching for novel ideas on custom serialization
- Jeroen Ooms for the Anticonf configure script
–
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.
