nanomsg 0.3.0

A high-level, Rust idiomatic wrapper around nanomsg.
docs.rs failed to build nanomsg-0.3.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: nanomsg-0.7.2

Rust Nanomsg Build Status

Cargo 0.3.0 MIT License

Nanomsg is a modern messaging library that is the successor to ZeroMQ, written in C by Martin Sustrik and colleagues. The nanomsg library is licensed under MIT/X11 license. "nanomsg" is a trademark of 250bpm s.r.o.

Requirements

You'll need to have nanomsg installed beforehand.

Getting Started

This library is Cargo supported! Simply add a new cargo dependency and away you go!

[dependencies.nanomsg]
git = "https://github.com/thehydroimpulse/nanomsg.rs"

Now you can use the crate after you include it:

extern crate nanomsg;

Creating a Socket (Push/Pull)

The basis of Nanomsg is a Socket. Each socket can be of a certain type. The type of a socket defines it's behaviour and limitations (such as only being able to send and not receive).

use nanomsg::{Socket, Protocol, NanoResult};

fn initialize() -> NanoResult<()> {
    let mut socket = try!(Socket::new(Protocol::Pull));
    let mut endpoint = try!(socket.bind("ipc:///tmp/pipeline.ipc"));

    loop {
        let msg = try!(socket.read_to_string());
        println!("We got a message: {}", msg.as_slice());
    }
}

fn main() {
    initialize();
}

As you can see, we bind a socket to an endpoint. These resources are destroyed correctly at the end of their lifetime, closing the bound socket.

We can also create a client to that socket. The Pull protocol is one that can only receive (push/pull), so we need the accompanying socket Push for this to work correctly (or to get a message in the end).

(For now, we'll create them as separate binaries because of some threading issues with nanomsg)

use nanomsg::{Socket, Protocol, NanoResult};

fn initialize() -> NanoResult<()> {
    let mut socket = try!(Socket::new(Protocol::Push));
    let mut endpoint = try!(socket.connect("ipc:///tmp/pipeline.ipc"));

    socket.write(b"message in a bottle");

    endpoint.shutdown();
}

fn main() {
    initialize();
}

Contributors

(In arbitrary order):

License

This project is under the same license as Rust. Dual MIT and Apache 2.

The MIT License (MIT)

  • Copyright (c) 2013-2014 Jason E. Aten, Ph.D. @glycerine

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.