Learn R Programming

⚠️There's a newer version (1.5.2) of this package.Take me there.

nanonext

R binding for NNG (Nanomsg Next Gen), a successor to ZeroMQ. NNG is a socket library implementing ‘Scalability Protocols’, a reliable, high-performance standard for common communications patterns including publish/subscribe, request/reply and service discovery, over in-process, IPC, TCP, WebSocket and secure TLS transports.

As its own threaded concurrency framework, provides a toolkit for asynchronous programming and distributed computing, with intuitive ‘aio’ objects which resolve automatically upon completion of asynchronous operations, and synchronisation primitives allowing R to wait upon events signalled by concurrent threads.

Designed for performance and reliability, the NNG library is written in C and nanonext is a lightweight zero-dependency wrapper.

Provides the interface for code and processes to communicate with each other - receive data generated in Python, perform analysis in R, and send results to a C++ program – on the same computer or across networks spanning the globe.

Implemented scalability protocols:

Supported transports:

  • inproc (intra-process)
  • IPC (inter-process)
  • TCP (IPv4 or IPv6)
  • WebSocket
  • TLS (over TCP and WebSocket)

Development of the TLS implementation was generously supported by the .

Web utilities:

  • ncurl - (async) http(s) client
  • stream - secure websockets client / generic low-level socket interface
  • sha224|256|384|512 - cryptographic hash and HMAC algorithms
  • base64enc|dec - base64 encoding and decoding
  • messenger() - console-based instant messaging with authentication

Installation

Install the latest release from CRAN:

install.packages("nanonext")

or the development version from rOpenSci R-universe:

install.packages("nanonext", repos = "https://shikokuchuo.r-universe.dev")

Quick Start

nanonext offers 2 equivalent interfaces: a functional interface, and an object-oriented interface.

Functional Interface

The primary object in the functional interface is the Socket. Use socket() to create a socket and dial or listen at an address. The socket is then passed as the first argument of subsequent actions such as send() or recv().

Example using Request/Reply (REQ/REP) protocol with inproc transport: (The inproc transport uses zero-copy where possible for a much faster solution than alternatives)

Create sockets:

library(nanonext)

socket1 <- socket("req", listen = "inproc://nanonext")
socket2 <- socket("rep", dial = "inproc://nanonext")

Send message from ‘socket1’:

send(socket1, "hello world!")
#> [1] 0

Receive message using ‘socket2’:

recv(socket2)
#> [1] "hello world!"

Object-oriented Interface

The primary object in the object-oriented interface is the nano object. Use nano() to create a nano object which encapsulates a Socket and Dialer/Listener. Methods such as $send() or $recv() can then be accessed directly from the object.

Example using Pipeline (Push/Pull) protocol with TCP/IP transport:

Create nano objects:

library(nanonext)

nano1 <- nano("push", listen = "tcp://127.0.0.1:5555")
nano2 <- nano("pull", dial = "tcp://127.0.0.1:5555")

Send message from ‘nano1’:

nano1$send("hello world!")
#> [1] 0

Receive message using ‘nano2’:

nano2$recv()
#> [1] "hello world!"

Vignette

Please refer to the nanonext vignette for full package functionality.

This may be accessed within R by:

vignette("nanonext", package = "nanonext")

Building from Source

Linux / Mac / Solaris

Installation from source requires ‘libnng’ >= v1.5.0 and ‘libmbedtls’ >= 2.5.0 (suitable installations are automatically detected), or else ‘cmake’ to compile ‘libnng’ v1.7.2 and ‘libmbedtls’ v3.5.2 included within the package sources.

It is recommended for optimal performance and stability to let the package automatically compile bundled versions of ‘libmbedtls’ and ‘libnng’ during installation. To ensure the libraries are compiled from source even if system installations are present, set the NANONEXT_LIBS environment variable prior to installation e.g. by Sys.setenv(NANONEXT_LIBS = 1).

As system libraries, ‘libnng’ is available as libnng-dev (deb) or nng-devel (rpm), and ‘libmbedtls’ as libmbedtls-dev (deb) or libmbedtls-devel (rpm). The INCLUDE_DIR and LIB_DIR environment variables may be set prior to package installation to specify a custom location for ‘libmbedtls’ or ‘libnng’ other than the standard filesystem locations.

Additional requirements for Solaris: (i) the ‘xz’ package - available on OpenCSW, and (ii) a more recent version of ‘cmake’ than available on OpenCSW - refer to the ‘cmake’ website for the latest source file.

Windows

For R >= 4.2 using the ‘Rtools42’ or ‘Rtools43’ toolchains, ‘libnng’ v1.7.2 and ‘libmbedtls’ v3.5.2 will be automatically compiled from the package sources during installation.

For previous R versions, pre-compiled ‘libnng’ v1.7.2 and ‘libmbedtls’ v3.5.2 libraries are downloaded and used for installation instead.

Acknowledgements and Links

We would like to acknowledge in particular:

  • Garrett D’Amore, author of the NNG library, for being generous with advice and implementing a feature request specifically for a more efficient ‘aio’ implementation in {nanonext}.
  • The R Consortium for funding the development of the secure TLS capabilities in the package, and Henrik Bengtsson and Will Landau’s roles in making this possible.
  • R Core for various auxiliary functions for serialisation and raw / character conversion, which have been adopted by the package.
  • Luke Tierney and Mike Cheng for meticulous documentation of the R serialization mechanism, which led to the package’s own implementation of a low-level interface to R serialization.
  • Jeroen Ooms - for his ‘Anticonf (tm)’ configure script, on which our original ‘configure’ was based, although much modified since.

Links:

nanonext website: https://shikokuchuo.net/nanonext/ nanonext on CRAN: https://cran.r-project.org/package=nanonext

nanonext is listed in CRAN Task Views: - High Performance Computing: https://cran.r-project.org/view=HighPerformanceComputing - Web Technologies: https://cran.r-project.org/view=WebTechnologies

NNG website: https://nng.nanomsg.org/ Mbed TLS website: https://www.trustedfirmware.org/projects/mbed-tls/

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.

Copy Link

Version

Install

install.packages('nanonext')

Monthly Downloads

5,735

Version

0.13.2

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Charlie Gao

Last Published

March 1st, 2024

Functions in nanonext (0.13.2)

nano

Create Nano Object
nanonext-package

nanonext: NNG (Nanomsg Next Gen) Lightweight Messaging Library
msleep

Sleep Utility
ncurl

ncurl
.unresolved

Technical Utility: Query if an Aio is Unresolved
listen

Listen to an Address from a Socket
is_error_value

Error Validators
.context

Technical Utility: Open Context
lock

Lock / Unlock a Socket
ncurl_aio

ncurl Async
messenger

Messenger
mclock

Clock Utility
nng_error

Translate Error Codes
opt

Get and Set Options for a Socket, Context, Stream, Listener or Dialer
protocols

Protocols [Documentation]
parse_url

Parse URL
pipe_notify

Pipe Notify
random

Random Data Generation
reap

Reap
ncurl_session

ncurl Session
nng_version

NNG Library Version
next_config

Configure Next Mode
reply

Reply over Context (RPC Server for Req/Rep Protocol)
request

Request over Context (RPC Client for Req/Rep Protocol)
send_aio

Send Async
send

Send
start

Start Listener/Dialer
stat

Get Statistic for a Socket, Listener or Dialer
socket

Open Socket
sha256

Cryptographic Hashing Using the SHA-2 Algorithms
transports

Transports [Documentation]
subscribe

Subscribe / Unsubscribe Topic
stop_aio

Stop Asynchronous Aio Operation
tls_config

Create TLS Configuration
survey_time

Set Survey Time
recv

Receive
strcat

Concatenate Strings
recv_aio

Receive Async
status_code

Translate HTTP Status Codes
stream

Open Stream
unresolved

Query if an Aio is Unresolved
write_cert

Generate Self-Signed Certificate and Key
call_aio

Call the Value of an Asynchronous Aio Operation
is_aio

Validators
%~>%

Signal Forwarder
cv

Condition Variables
context

Open Context
close.nanoContext

Close Connection
dial

Dial an Address from a Socket
base64enc

Base64 Encode / Decode