From the NNG Github Repository:
NNG, like its predecessors nanomsg (and to some extent ZeroMQ), is a lightweight, broker-less library, offering a simple API to solve common recurring messaging problems, such as publish/subscribe, RPC-style request/reply, or service discovery. The API frees the programmer from worrying about details like connection management, retries, and other common considerations, so that they can focus on the application instead of the plumbing.
This crate provides a safe wrapper around the NNG library, seeking to maintain an API that is similar to the original library. As such, the majority of examples available online should be easy to apply to this crate.
The current version requires Rustc v1.31 or greater. In general, this crate should always be able to compile with the Rustc version available on the oldest Ubuntu LTS release. Any change that requires a newer Rustc version will always be considered a breaking change and this crate's version number will be bumped accordingly.
The following example uses the intra-process transport to set up a request/reply socket pair. The "client" sends a String to the "server" which responds with a nice phrase.
```rust use nng::*;
// Set up the server and listen for connections on the specified address. let address = "inproc://nng/lib.rs"; let server = Socket::new(Protocol::Rep0).unwrap(); server.listen(address).unwrap();
// Set up the client and connect to the specified address let client = Socket::new(Protocol::Req0).unwrap(); client.dial(address).unwrap();
// Send the request from the client to the server. let request = b"Ferris"[..].into(); client.send(request).unwrap();
// Receive the message on the server and send back the reply let request = { let req = server.recv().unwrap(); String::fromutf8(req.tovec()).unwrap() }; asserteq!(request, "Ferris"); let reply = format!("Hello, {}!", request).asbytes().into(); server.send(reply).unwrap();
// Get the response on the client side. let reply = { let rep = client.recv().unwrap(); String::fromutf8(rep.tovec()).unwrap() }; assert_eq!(reply, "Hello, Ferris!"); ```
Additional examples are in the examples
directory.