Rust-Coap rs: coap-rs Constrained Application Protocol(CoAP) Covertness/coap-rs — A Constrained Application Protocol(CoAP) library for Rust.


Travis Build Status Windows Build Status Downloads Coverage Status MIT licensed

A fast and stable Constrained Application Protocol(CoAP) library implemented in Rust.




First add this to your Cargo.toml:

coap = "0.8"

Then, add this to your crate root:

extern crate coap;




extern crate coap;

use coap::{Server, IsMessage, Method};
use tokio::runtime::Runtime;

fn main() {
    let addr = "";

    Runtime::new().unwrap().block_on(async move {
        let mut server = Server::new(addr).unwrap();
        println!("Server up on {}", addr);
        move |request| {
            match request.get_method() {
                &Method::Get => println!("request by get {}", request.get_path()),
                &Method::Post => println!("request by post {}", String::from_utf8(request.message.payload).unwrap()),
                &Method::Put => println!("request by put {}", String::from_utf8(request.message.payload).unwrap()),
                _ => println!("request by other method"),
            return match request.response {
                Some(mut message) => {
                _ => None


extern crate coap;

use coap::{CoAPClient, CoAPResponse};

fn main() {
    let url = "coap://";
    println!("Client request: {}", url);

    let response = CoAPClient::get(url).unwrap();
    println!("Server reply: {}", String::from_utf8(response.message.payload).unwrap());


$ cargo bench


  • Custom Option IDs
    Custom Option IDs

    Apr 12, 2018

    It would be good to have the ability to add a custom option.

    A quick and dirty way could be to add to the CoapOption Enum: Custom(u8)

  • Async client requests
    Async client requests

    Dec 10, 2018

    It appears that the underlying IO library is asynchronous, and the server implementation does indeed expose an asynchronous handler interface.

    It would be nice if the client side could also be made to have an asynchronous interface rather than having to do a blocking read.

  • Support for rfc8323 (CoAP over TCP)
    Support for rfc8323 (CoAP over TCP)

    Jan 24, 2019

    This is more of a question, rather than an issue, but curious if there are any plans to support TCP RFC ( for coap-rs.

  • WIP: Random initial message id
    WIP: Random initial message id

    Mar 7, 2019

    Should be merged after

  • Block 1 Option
    Block 1 Option

    Mar 9, 2019

    Also refactored so that options-code is now in a separate file. I think as this grows having everything in would be cumbersome. Hope you like it.

  • Block transfer
    Block transfer

    Mar 17, 2019


    Some things left to be done:

    • MessageID for all messages are 0 -> Suggestions on how to best fix this are welcome. I made a proposal a while back

    • Should parse and respect Block1 block size suggestions in the response from the server: According to Section 2.3 in

    • The current implementation is just one function: block_transfer::send(). It could be a member function of the CoAPClient, should it?

  • Provide Routing tool
    Provide Routing tool

    Jun 18, 2016

    It would be nice to have a tool to handle request routing. One good example is Iron's router functionality. This prevents every user from having to reimplement this functionality.


  • static handler brings problems
    static handler brings problems

    Jul 8, 2016

    The CoAPServer::handle takes a 'static handler.

    I'm a bit new to rust, but I believe this means we can only use top level methods as handler, and then all state needed for the server will need to be stored in global variables?

    As far as I understand it, this will also make the routing tool in #4 impossible.

    Please correct me if I'm wrong!

  • Fix: Make URLs with IPv6 addresses usable
    Fix: Make URLs with IPv6 addresses usable

    Aug 10, 2018

    Previously, in parsing URLs, the latters' domains were considered instead of their literal hosts, which would fail for IPv6 addresses (and presumably also IPv4 addresses) that failed to reverse-look-up.

    Now, the literal host is used instead, stripped of encapsulating square brackets (denoting an IPv6 address), if any.

    This resolves #24.

  • Packet refactor
    Packet refactor

    Jul 1, 2016

    Still planning to add some tests, but please feel free to review. I can also squash the commits once we are ready (I included them in case you wanted to see the steps).

  • Pass socket to handler callback
    Pass socket to handler callback

    Jun 29, 2016

    For some applications it's necessary to have access to the sender from the callback. It would be useful to have the SocketAddr in the handler signature.

  • Respond from the CoAP port to support Copper
    Respond from the CoAP port to support Copper

    Jun 1, 2016

    The Mozilla Copper plugin only recognizes messages that return from the CoAP port. Previously, responses were returned from a random UDP socket.

    I have added a "CoAPResponse" class which is based on the CoAPClient class. This allows for responses from the server to be on the same port that requests were sent to.