TMQ - Rust ZeroMQ bindings for Tokio
This crate bridges Tokio and ZeroMQ to allow for ZeroMQ in the async world.
Currently a WIP
Currently Implemented Sockets
- Request
- Response
- Subscribe
- Publish
Examples
There are two sets of examples, publish/subscribe
and request/response
.
Bring up two terminals and run either:
# In Another Terminal
Or:
# Another Terminal
Usage
Usage is made to be simple, but opinionated. See the examples for working code, but in general, you need to import tokio
and tmq::*
Request
A request is a Stream
takes an input stream of Messages (using the with
function), sends them to a response socket, and then returns the messages as a stream.
let request = request
.connect
.expect
.with
.for_each.map_err;
run;
Response
A response socket is a Future
that receives messages, responds to them, and sends them back as per the Responder
implementation:
let responder = respond
.bind
.expect
.with.map_err;
run;
Responder trait
The with
function takes anything that implements the Responder
trait or a closure as above:
//You can use a struct to respond by implementing the `Responder` trait
//Or you can use a free-floating function
Publish
A publish socket is a Sink
that takes values, and sends them to any subscribe sockets connected (note: ZeroMQ will drop messages if noone is connected).
let mut i = 0;
let broadcast = new_interval
.map;
let request = publish
.bind
.expect
.finish
.send_all
.map
.map_err;
run;
Subscribe
a subscribe socket is a Stream
that reads in values from a publish socket. You specify the filter prefix using the subscribe
method, using ""
for all messages.
let request = subscribe
.connect
.expect
.subscribe
.for_each.map_err;