Trait tokio_zmq::prelude::StreamSocket[][src]

pub trait StreamSocket: AsSocket {
    fn recv(self) -> MultipartResponse<Self> { ... }
fn stream(self) -> MultipartStream { ... } }

This trait provides the basic Stream support for ZeroMQ Sockets. It depends on AsSocket, but provides implementations for sink and recv.

Provided Methods

Receive a single multipart message from the socket.

Example, using the Rep wrapper type

#![feature(try_from)]

extern crate futures;
extern crate tokio;
extern crate tokio_zmq;
extern crate zmq;

use std::{convert::TryInto, sync::Arc};

use futures::Future;
use tokio_zmq::{prelude::*, async::MultipartStream, Error, Multipart, Rep, Socket};

fn main() {
    let context = Arc::new(zmq::Context::new());
    let rep: Rep = Socket::builder(context)
        .connect("tcp://localhost:5568")
        .try_into()
        .unwrap();

    let fut = rep.recv().and_then(|(multipart, _)| {
        for msg in &multipart {
            if let Some(msg) = msg.as_str() {
                println!("Message: {}", msg);
            }
        }
        Ok(multipart)
    });

    // tokio::runtime::run2(fut.map(|_| ()).or_else(|e| {
    //     println!("Error: {}", e);
    //     Ok(())
    // }));
}

Receive a stream of multipart messages from the socket.

Example, using a Sub wrapper type

#![feature(try_from)]

extern crate zmq;
extern crate futures;
extern crate tokio;
extern crate tokio_zmq;

use std::{convert::TryInto, sync::Arc};

use futures::{Future, Stream};
use tokio_zmq::{prelude::*, async::MultipartStream, Error, Multipart, Socket, Sub};

fn main() {
    let context = Arc::new(zmq::Context::new());
    let sub: Sub = Socket::builder(context)
        .connect("tcp://localhost:5569")
        .filter(b"")
        .try_into()
        .unwrap();

    let fut = sub.stream().for_each(|multipart| {
        for msg in multipart {
            if let Some(msg) = msg.as_str() {
                println!("Message: {}", msg);
            }
        }
        Ok(())
    });

    // tokio::runtime::run2(fut.map(|_| ()).or_else(|e| {
    //     println!("Error: {}", e);
    //     Ok(())
    // }));
}

Implementors