Expand description
Rust high-level wrapper around NNG (Nanomsg-Next-Gen):
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.
Features:
- Use nng_aio for asynchronous I/O
- Use nng_ctx for advanced protocol handling
- Leverage futures crate for ease of use with tokio and eventual support of
async
/await
§Examples
Simple:
use runng::{
Dial, Listen, RecvSocket, SendSocket,
factory::latest::ProtocolFactory,
msg::NngMsg,
protocol::*,
};
fn simple_reqrep() -> Result<(), runng::Error> {
const url: &str = "inproc://test";
let factory = ProtocolFactory::default();
let mut rep = factory.replier_open()?;
rep.listen(&url)?;
let mut req = factory.requester_open()?;
req.dial(&url)?;
req.sendmsg(NngMsg::new()?)?;
rep.recvmsg()?;
Ok(())
}
Asynchronous I/O:
use futures::{
executor::block_on,
future::Future,
stream::Stream,
};
use runng::{
Dial, Listen,
asyncio::*,
factory::latest::ProtocolFactory,
msg::NngMsg,
protocol::*,
};
fn async_reqrep() -> Result<(), runng::Error> {
const url: &str = "inproc://test";
let factory = ProtocolFactory::default();
let mut rep_sock = factory.replier_open()?;
let mut rep_ctx = rep_sock.listen(&url)?.create_async()?;
let mut req_sock = factory.requester_open()?;
let mut req_ctx = req_sock.dial(&url)?.create_async()?;
let req_future = req_ctx.send(NngMsg::new()?);
let _request = block_on(rep_ctx.receive())?;
block_on(rep_ctx.reply(NngMsg::new()?))?;
block_on(req_future)?;
Ok(())
}
Additional examples in examples/
folder.
Re-exports§
pub use self::mem::NngString;
pub use self::ctx::*;
pub use self::factory::*;
pub use self::options::*;
pub use self::result::*;
pub use self::socket::*;
Modules§
- asyncio
- Asynchronous I/O with
nng_aio
. - ctx
- Protocol contexts (
nng_ctx
). - dialer
- Dialers connect to listeners.
- factory
- Factories for specific sets of protocols.
- listener
- Listeners accept connections from dialers.
- mem
- Raw memory allocations.
- msg
- Messages.
- options
- NNG options.
- pipe
- Pipe
- protocol
- NNG protocols. See Section 7.
- result
- Return values and error handling
- socket
- Socket basics
- stats
- Runtime statistics
- transport
- Transports like TCP, named pipes, etc.