Giter VIP home page Giter VIP logo

tmq's Introduction

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:

cargo run --example publish
# In Another Terminal
cargo run --example subcribe

Or:

cargo run --example request
# Another Terminal
cargo run --example response

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(&Context::new())
    .connect("tcp://127.0.0.1:7899")
    .expect("Couldn't connect")
    .with(stream::iter_ok(vec!["Message1", "Message2", "Message3"].into_iter().map(|val| Message::from(val))))
    .for_each(|val| {
        info!("Response: {}", val.as_str().unwrap_or(""));
        Ok(())
    }).map_err(|err| {
        error!("Error with request: {}", err);
    });

tokio::run(request);

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(&Context::new())
    .bind("tcp://127.0.0.1:7899")
    .expect("Couldn't bind address")
    .with(|msg: Message| {
        info!("Request: {}", msg.as_str().unwrap_or(""));
        Ok(msg)
    }).map_err(|err| {
        error!("Error from server:{}", err);
    });

tokio::run(responder);

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
pub struct EchoResponder {}

impl Responder for EchoResponder {
    type Output = FutureResult<zmq::Message, Error>;

    fn respond(&mut self, msg: zmq::Message) -> Self::Output {
        return Ok(msg).into();
    }
}

//Or you can use a free-floating function
fn echo(msg: zmq::Message) -> impl Future<Item = zmq::Message, Error = Error> {
    return ok(msg);
}

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 = Interval::new_interval(Duration::from_millis(1000))
    .map(move |_| {
        i += 1;
        let message = format!("Broadcast #{}", i);
        Message::from(&message)
    });

let request = publish(&Context::new())
    .bind("tcp://127.0.0.1:7899")
    .expect("Couldn't bind")
    .finish()
    .send_all(broadcast)
    .map(|_| ())
    .map_err(|e| {
        error!("Error publishing:{}", e);
    });

tokio::run(request);

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(&Context::new())
    .connect("tcp://127.0.0.1:7899")
    .expect("Couldn't connect")
    .subscribe("")
    .for_each(|val| {
        info!("Subscribe: {}", val.as_str().unwrap_or(""));
        Ok(())
    }).map_err(|e| {
        error!("Error Subscribing: {}", e);
    });

tmq's People

Contributors

cetra3 avatar

Watchers

James Cloos avatar Jeremy Lempereur avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.